Home > Web Front-end > JS Tutorial > body text

JavaScript uses constructors and prototypes to simulate the functions of C# classes_javascript skills

WBOY
Release: 2016-05-16 16:56:53
Original
1295 people have browsed it

Copy code The code is as follows: age) {
this.name = name;
this.age = age;
}
//Define person prototype, the attributes in the prototype can be referenced by custom objects
person.prototype = {
                                                                                                                                                                                                                               
                                                                      🎜>





Copy code


The code is as follows:

This requires the introduction of another concept - Prototype, we can simply think of prototype as a template. The newly created custom objects are all copies of this template (prototype) (actually not a copy but a link, but this link is not It can be seen that it feels like a copy to people).
JavaScript simulates the functions of classes through constructors and prototypes.   window.onload = function () {     person.prototype.sex = 'Male';    var fmj =new person('kkk', 22); :' fmj.sex);                                                                                ; Three output: 'fmj.sex); // Output results on the console of the debug. //console.log (fmj.getage ()); 🎜>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template