JavaScript simulates the functionality of a C # class using constructors and prototypes

Source: Internet
Author: User

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

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.