實體物件使用屬性和方法層層的搜索:
實體物件使用的屬性或者方法, 先在實體中查找, 找到了則直接使用; 找不到則, 再去實體物件的__proto__指向的原型物件prototype中找, 找到了則使用, 找不到則報錯,<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> <script> function Person(age, sex) { this.age = age;//年齡 this.sex = sex; // this.eat = function () { // console.log("建構式中的吃"); // }; } Person.prototype.sex = "女"; Person.prototype.eat = function () { console.log("原型物件中的吃"); }; var per = new Person(20, "男"); console.log(per.sex);//男 實體化的屬性或方法,現在實體物件里面找 per.eat(); //"原型物件中的吃" 實體物件中找不到的時候,再去原型物件中找 console.dir(per); </script> </head> <body> </body> </html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/159145.html
標籤:JavaScript
上一篇:JS高級---原型的簡單的語法
