The
This article mainly introduces JavaScript's ability to simulate C # classes using constructors and prototypes. Need friends can come to the reference, I hope to help you.
The code is as follows: //constructor function person (name, age) { THIS.name = name; this.age = age; } //define person prototype, attributes in prototype can be customized by custom object reference person . prototype = { getname:function () { return this.name; , getage:function () { return this.age; { } code as follows: This requires the introduction of another concept-prototype (prototype), we can simply Consider prototype as a template, and the newly created custom object is a copy of the template (prototype) (actually not a copy but a link, except that the link is invisible and feels like a copy). JavaScript simulates the functionality of a class by constructing functions and prototypes. window.onload = function () { person.Prototype.sex = ' Male '; var FMJ =new person (' KKK ', 22); alert (' First output: ' +fmj.sex '); Fmj.sex = ' confidential '; alert (' Second output: ' + fmj.sex); Delete fmj.sex; alert (' Third output: ' + fmj.sex); //Output results in debug console. //console.log (Fmj.getage ()); }