getter与setter
var obj = { get a() { return 2; }, get c() { return 34 } } Object.defineProperty( obj, 'b', { get: function () { return this.a * 2 }, enumerable: true } ) console.log(obj); /* * a: 2 * b: 4 * c: 34 * get a: ƒ a() * get b: ƒ () * get c: ƒ c() //有set就得有get 不然会出现去怪问题 var obj1 = { get a() { return this._a_; },//有点像初始化变量 set a(val) { this._a_ = val + 4; }//类似于变量的赋值 如果没有set obj1.a为undefined 就算下面赋值也不行 }; obj1.a = 3; console.log(obj1.a) /*对象的属性操作符 是否可以枚举*/ var obj2 = {name:'hxq'}; Object.defineProperty( obj2, 'a', { value:2, enumerable:true } ) Object.defineProperty( obj2, 'b', { value:3, enumerable:false } ) console.log(obj2); /*for in 遍历下标*/ for (let i in [1,1,1,3,232,3]){ console.log(i) //可以通过遍历下标来遍历值 } // for of 直接遍历值 for (let i of [21312,,1,31,3,123,1]){ console.log(i) } /*for of 原理内置的@@iterator来手动遍历数组*/ var arr=[12,231,32,32]; var it=arr[Symbol.iterator](); console.log(it) console.log(it.next());//{value: 12, done: false} console.log(it.next());//{value: 231, done: false} console.log(it.next());//{value: 32, done: false} console.log(it.next());//{value: 32, done: false} console.log(it.next());//{value: undefin, done: true}