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

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.

主站蜘蛛池模板: 涿鹿县| 金秀| 湘潭市| 砀山县| 邳州市| 开原市| 科尔| 舒兰市| 崇仁县| 玉树县| 云霄县| 山东省| 灵宝市| 永宁县| 囊谦县| 乐至县| 兰西县| 铜鼓县| 昌乐县| 垣曲县| 陇西县| 新和县| 仁怀市| 吉首市| 永定县| 阜新市| 铜梁县| 阿拉善盟| 天峻县| 兰西县| 淮北市| 西林县| 晋州市| 太和县| 额尔古纳市| 镶黄旗| 桐柏县| 凤翔县| 循化| 吉安市| 遂平县|