js中的原型对象

it2024-05-12  43

js中的原型对象 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js中的原型对象</title> <script type="text/javascript"> function Person(name , age , gender){ this.name = name; this.age = age; this.gender = gender; this.sayName = function(){ console.log(this.name); }; } //公有方法可以给对象的ptototype属性增加 Person.prototype.caicai = function(){ console.log("oasdaofad") } Person.prototype.xiao = "haha"; var p1 = new Person("hahaha",22); p1.sayName(); p1.caicai(); console.log(p1.__proto__ == Person.prototype) // true console.log(typeof p1.__proto__.__proto__.__proto__) //object console.log(name in p1)//false console.log("name" in p1) //true console.log(p1.hasOwnProperty("name"))//true console.log(p1.hasOwnProperty("xiao")) //false </script> </head> <body> </body> </html>
最新回复(0)