Copy Code code as follows:
Constructors
function person (name, age) {
THIS.name = name;
This.age = age;
}
Defines the person prototype, where attributes can be referenced by custom objects
Person.prototype = {
Getname:function () {
return this.name;
},
Getage:function () {
return this.age;
}
}
Copy Code code as follows:
This requires the introduction of another concept-prototype (prototype), we can simply think of prototype as a template, the newly created custom object is a copy of this template (prototype) (actually not a copy but a link, except that the link is not visible, It feels like a copy to the people.
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 the results in the debug console.
Console.log (Fmj.getage ());
}