JavaScript uses constructors and prototypes to simulate the functions _javascript techniques of C # classes

Source: Internet
Author: User

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 ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.