js对象原型与in操作符

来源:互联网 发布:先锋网络电视手机软件 编辑:程序博客网 时间:2024/06/08 09:51
//hasOwnProperty()可以检测一个对象的属性是不是存在于实例中//in   有俩种使用in操作符,单独使用和在for-in循环使用。单独使用的时候,in操作符会在通过对象能够访问给定属性时返回true,无论该属性存在于实例中还是原形中;function Person(){};Person.prototype.name = "zhangjing";Person.prototype.age = "18";Person.prototype.sayName = function(){    console.log(this.name);};var person1 = new Person();person1.name = 'zhangsan';console.log(person1.name);          //zhangjing(来自于实例)console.log(person1.hasOwnProperty("name"));   //该属性是否存在于实例中,trueconsole.log("name" in person1);      //该对象中是否有name属性,truedelete(person1.name);console.log(person1.name);          //zhangjing(来自于原型)console.log(person1.hasOwnProperty("name"));   //该属性是否存在于实例中,falseconsole.log("name" in person1);      //该对象中是否有name属性,true


原创粉丝点击