標籤:
1.__proto__每個對象都有一個__proto__屬性,指向該對象的原型對象
<script> var person = function(name,city){ this.name = name; this.city =city; } person.prototype.showName = function(){ return this.name; } var p = new person(‘pmx‘,‘shanghai‘); console.log(p.__proto__ == person.prototype);</script>
2.isPrototypeOf(obj)用來判斷當前對象是否是obj的原型對象
<script> var person = function(name,city){ this.name = name; this.city =city; } person.prototype.showName = function(){ return this.name; } var p = new person(‘pmx‘,‘shanghai‘); console.log(person.prototype.isPrototypeOf(p));</script>3.getPrototypeOf(obj)得到obj的原型對象
<script> var person = function(name,city){ this.name = name; this.city =city; } person.prototype.showName = function(){ return this.name; } var p = new person(‘pmx‘,‘shanghai‘); console.log(Object.getPrototypeOf(p) == person.prototype);</script>
4.hasOwnProperty用來判斷屬性是存在於本身還是原型對象中
<script> var person = function(name,city){ this.name = name; this.city =city; } person.prototype.age = 26; person.prototype.showName = function(){ return this.name; } var p = new person(‘pmx‘,‘shanghai‘); console.log(p.hasOwnProperty("name")); console.log(p.hasOwnProperty("age"));</script>
5.in用來判斷obj是否有指定屬性,不論該屬性存在於對象上,還是原型鏈上
<script> var person = function(name,city){ this.name = name; this.city =city; } person.prototype.age = 26; person.prototype.showName = function(){ return this.name; } var p = new person(‘pmx‘,‘shanghai‘); console.log("name" in p); console.log("age" in p);</script>
JS之理解對象