
1、每一個建構式都包含一個prototype屬性,指向另一個物件,這個prototype就是一個物件,這個物件的所有屬性和方法,都會被建構式所擁有,
// 建構式
function Person(uname, age) {
this.uname = uname;
this.age = age;
}
let person = new Person("小黑", 19);
Person.prototype.sex = '未知'; // 在原型物件上定義屬性或方法
Person.sex = '男'; // 靜態成員
2、實體物件都會有一個屬性__proto__ 指向prototype原型物件
console.log(Person.prototype === person.__proto__); // true
3、原型物件(prototype)和物件原型(proto)內都有一個 constructor 屬性,稱為建構式,因為它指回建構式本身
console.log(Person.prototype.constructor === person.__proto__.constructor); // true
4、原型鏈和成原查找機制就近原則:首先查找物件本身有的屬性
console.log(person.sex); // 未知 去原型物件中查找
person.sex = '女'; // 建構式成員
console.log(person.sex); // 女 就近原則
console.log(Person.sex); // 男 靜態成員只能通過建構式訪問
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/183353.html
標籤:其他
上一篇:區塊鏈上智能合約的講解
