随题而学(二)多维数组转一维数组
多维数组转一维数组:[1,[2,3]] ==》 [1,2,3]
function tran(array){
if(Object.prototype.toString.call(array) != ‘[object Array]‘){
alert(Object.prototype.toString.call(array) + "非数组")
return ;
}
var newArr = [];
function trans(arr){
for (var num = 0; num < arr.length;num ++){
if(arr[num].length){
trans(arr[num]);
} else {
newArr.push(arr[num]);
}
}
}
trans(array);
return newArr;
}
Object.prototype.toString 与 typeof 区别:
typeof
- 在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 "object"。
- Object.prototype.toString ( )
- When the toString method is called, the following steps are taken:
- If the this value is undefined, return "[object Undefined]".
- If the this value is null, return "[object Null]".
- Let O be the result of calling ToObject passing the this value as the argument.
- Let class be the value of the [[Class]] internal property of O.
- Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".
参考:http://my.oschina.net/sfm/blog/33197,http://blog.csdn.net/spy19881201/article/details/12192767,http://www.cnblogs.com/Aralic/p/4397015.html
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。