官术网_书友最值得收藏!

Prototyped

JavaScript uses prototypes instead of classes for inheritance. It is possible to emulate all OOP characteristics using just prototypes:

function Person(first, last, age) {
this.firstName = first;
this.lastName = last;
this.age = age;
}

var diego = new Person('Diego', 'Arguelles', 26)
diego.nationality = 'Peruvian'
console.log(diego)
// Person {firstName: "Diego", lastName: "Arguelles", age: 26, nationality: "Peruvian"}

Person.prototype.career = 'Engineering'
console.log(diego.career) // Engineering

That being said, what is exactly a prototype? Different from objects, one prototype does not have a closed structure. In objects, we define standard properties and we just have these properties for work, since JavaScript is not completely an object-oriented language, we have the advantage to add, remove, or change properties and values of our prototypes depending on our needs.

We can modify prototype attributes at runtime. Note that even if you can modify any prototype, you should only modify yours. If you modify standard prototypes (for example, the  array prototype) you will encounter very weird bugs in your application.

主站蜘蛛池模板: 灵石县| 温州市| 广东省| 长沙市| 陈巴尔虎旗| 共和县| 太湖县| 松潘县| 双柏县| 浦城县| 巧家县| 元阳县| 特克斯县| 建昌县| 青河县| 吴旗县| 铁岭县| 达日县| 五华县| 抚州市| 顺义区| 巩留县| 通山县| 湘乡市| 石林| 常山县| 天门市| 平果县| 收藏| 忻州市| 河源市| 诸城市| 泗水县| 九寨沟县| 安化县| 晋江市| 肃南| 共和县| 宁明县| 靖远县| 宣汉县|