1.数组清空的方法javascript
var a = [1,2,3]; a.length = 0; //方法1 a.splice(0, a.length); //方法2
2.数组复制方法html
var a = [1,2,3]; a.slice(0)
1.判断对象是否为空java
Object.key.length==0 //为空 ES6
2.对象复制数组
(1).万能办法函数
function clone(obj){ let temp = null; if(obj instanceof Array){ temp = obj.concat(); }else if(obj instanceof Function){ //函数是共享的是无所谓的,js也没有什么办法能够在定义后再修改函数内容 temp = obj; }else{ temp = new Object(); for(let item in obj){ let val = obj[item]; temp[item] = typeof val == 'object'?clone(val):val; //这里也没有判断是否为函数,由于对于函数,咱们将它和通常值同样处理 } } return temp; }
(2).JSON对象序列化方法, 弊端: 不能复制函数prototype
JSON.parse(JSON.stringify(obj))
1.toString方法code
Object.prototype.toString.call(array) === '[object Array]' //true Object.prototype.toString.call(obj) === '[Object Object]' //true 数值:返回[object Number]。 字符串:返回[object String]。 布尔值:返回[object Boolean]。 undefined:返回[object Undefined]。 null:返回[object Null]。 数组:返回[object Array]。 arguments 对象:返回[object Arguments]。 函数:返回[object Function]。 Error 对象:返回[object Error]。 Date 对象:返回[object Date]。 RegExp 对象:返回[object RegExp]。 其余对象:返回[object Object]。
2.constructor方法htm
obj.constructor === Array//true obj.constructor === Object //true
3.instanceof方法, 弊端: 区分不开对象或者数组对象
obj instaceof Object //true array instaceof Object// true
4.isArray方法教程
Array.isArray([1,2,3]) //true
以上是我认为无懈可击的方法, 其余还有不少, 须要请留言
想了解原生js的"数组"和"对象"的方法, 请点击 JavaScript教程-阮一峰