Home  >  Article  >  Web Front-end  >  JavaScript uses constructors and prototypes to simulate the functions of C# classes_javascript skills

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

WBOY
WBOYOriginal
2016-05-16 16:56:531297browse

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 ()); 🎜>
Statement:
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