Javascript のすべての関数にはプロトタイプ属性があり、このプロトタイプ属性はオブジェクト型オブジェクトです。この関数によって構築されたすべてのオブジェクトは、このプロトタイプの特性を持ちます。つまり、構築されたオブジェクトを使用してプロトタイプのプロパティとメソッドに直接アクセスできます。の上。
次のコードは、プロトタイプの使用方法を示しています:
上記のプログラムを実行すると、プロトタイプのプロパティとメソッドが作成されたオブジェクト間で呼び出せることがわかります。さらに重要なのは、プロトタイプのプロパティとメソッドが同じオブジェクト間で共有されることです。 type
プロトタイプのもう 1 つの一般的な機能は、プロトタイプを通じてオブジェクトの継承関係を構築することです。基底クラスのオブジェクトをサブクラスのプロトタイプに割り当てることで、オブジェクト指向の継承関係をシミュレートできます。これは、JavaScript のオブジェクト指向メカニズムとよく呼ばれるものです。次のコード スニペットは、この機能を使用してオブジェクトを構築する際の継承関係を示しています。
次に、作成した Staff オブジェクトに __proto__ 属性を追加し、それを Function コンストラクターのプロトタイプに割り当てます。このステップは、var x = new X () メソッドなどを実行するときのすべてのステップです。 X のプロトタイプを :
From the above analysis, we can see that when an object is created, a private attribute __proto__ is created, and when a function is created, a prototype attribute is created. Because Staff is a function type object, it will have both properties.
These two properties are key properties for building a prototype chain. Let's analyze how the prototype is passed when executing the code var staff1 = new Staff("hunter").
According to the above analysis, staff1.__proto__ = Staff.prototype, and Staff.prototype is an object created by Object, that is, Staff.prototype.__proto__ = Object.prototype, so staff1.__proto__ .__proto__ points to Object. prototype, that is, staff1.__proto__ .__proto__ == Object.prototype, this is the prototype chain. When you want to read the properties of an object, JS first looks for whether the object itself has this property. If not, it will continue to search along the prototype chain. this property.
If you know the principle of prototype chain, it is easy to build object inheritance in Javascript based on this principle.
From the above analysis, we know that the top of the prototype chain is Object.prototype, which means that Object is the base class of all objects in the built inheritance relationship. You can run the following code verification.
Object.prototype.location = "China";
function Staff(name) { // Base class
this.name = name;
}
Staff.prototype.say = function() {
alert(this.name " say hello") ;
}
var ManStaff1 = new Staff("hunter");
var ManStaff2 = new Staff("dangjian");
alert(ManStaff1.location);
alert(ManStaff2. location);
The running results show that Object is the base class of Staff, so how to build a subclass of Staff?
Understanding the establishment principle of the above function, we can easily write the following code:
function Staff(name) { // Base class
this.name = name;
}
Staff.prototype.say = function() {
alert(this .name " say hello");
}
function ManStaff(name, age) { // Subclass
Staff.call(this,name);
this.age = age;
}
ManStaff.prototype = new Staff(); // Establish inheritance relationship
var ManStaff1 = new ManStaff("hunter", 22);
var ManStaff2 = new ManStaff("dangjian", 32) ;
ManStaff1.say();
ManStaff2.say();
This is the sentence that establishes the inheritance relationship: ManStaff.prototype = new Staff(); , the inheritance relationship is calculated as follows :ManStaff1.__proto__ = =ManStaff.prototype, ManStaff.prototype.__proto__ = Staff.prototype, Staff.prototype.__proto__ == Object.prototype; then ManStaff1.__proto__.__proto__.__proto__ == Object.prototype.
This inheritance relationship in JavaScript is looser than the traditional object-oriented inheritance relationship, and the construction method is more difficult to understand, but as a scripting language, its functions are already very powerful.