i具有以下变量,这些变量是数组:
const gumBrands = ['orbit', 'trident', 'chiclet', 'strident'];
const mintBrands = ['altoids', 'certs', 'breath savers', 'tic tac'];
下面我具有以下函数,该函数将变量用作输入参数:
function shallowCopy (arrOne, arrTwo) {
if (arrOne.constructor === 'Array'){
return [...arrOne, ...arrTwo];
}
else {
console.log('test this');
}
}
shallowCopy(gumBrands, mintBrands)
我希望我的代码返回:
[ 'orbit',
'trident',
'chiclet',
'strident',
'altoids',
'certs',
'breath savers',
'tic tac' ]
代码运行我的其他语句并返回: test this
我在做什么错?
.constructor
不包含字符串"Array"
,而是对全局Array
对象的引用。
请注意,阵列可以分类,并且它们的.constructor
不同。您可能需要考虑使用instanceof Array
。