用Object.definedproperties 一次性添加或修改多個屬性的特性和值。this
1 <script> 2 var obj ={} 3 Object.defineProperties(obj,{ 4 x:{value:1,writalbe:true,configurable:true,enumerable:true}, 5 y:{value:2,configurable:true} 6 }) 7 console.log(obj.x) //=>1 8 console.log(obj.y) //=>2 9 console.log(Object.getOwnPropertyDescriptor(obj,"x"))//獲得屬性的描述 value:1 writable:true configurable:true, enumerable:true 10 console.log(Object.getOwnPropertyDescriptor(obj,"y")) // value:2 writable:false configurable:true, enumerable:false 11 obj.z=3 //這個z是經過對象字面量的方式建立的 12 console.log(Object.getOwnPropertyDescriptor(obj,"z"))//因此全部的屬性特性都爲true 13 </script>
檢測對象是不是另外一個對象的原型(或者處於原型鏈中)spa
1 <script> 2 var obj={ 3 } //此處建立了一個空對象 4 var obj1 =Object.create(obj) //用object.create建立了一個新對象,把obj做爲obj1的原型 5 console.log(obj.isPrototypeOf(obj1)) //=> true 此時返回值就true,因obj是obj1的原型 6 console.log(Object.prototype.isPrototypeOf(obj))//=>true 由於object.prototype是頂級對象,是對象原型上的原型 7 console.log(Object.prototype.isPrototypeOf(obj1))//=>true 也是對象上的原型 8 </script>
對象類class是一個標識對象類型的字符串prototype
ECMAscript3和ECMAscript5都沒有定義此方法,能夠經過頂級對象的toString()方法code
js的內建對象都帶有toSting方法,因此要用一個CALL回調對象
代碼以下,對何標識對象類型的字符串:blog
1 <script> 2 function classof(obj){ 3 if(obj === null){ 4 return null 5 } 6 if(obj === undefined){ 7 return undefined 8 } 9 return Object.prototype.toString.call(obj).slice(8,-1) 10 } 11 var x = null; //=>null 12 var x = undefined; //=> undefined 13 var x =true //=> boolean 14 var x = 1234; //=> number 15 var x = "this is" //=> string 16 var x = new Array() //=> arry 17 var x = new Date() //=> date 18 console.log(classof(x)) 19 </script>