JavaScript Zhongyuan and Inheritance (simple example)

Source: Internet
Author: User

Create a custom object with the prototype prototype person:

functionPerson (name,sex) { This. Name =name;  This. Sex =sex;} Person.prototype={getName:function(){return  This. Name}, Getsex:function(){return  This. Sex}}varLiu =NewPerson ("LCY", "female");//Create a blank object//Copy the attributes in Person.prototype to an empty object (internally implemented as a hidden link)//pass the object through the This keyword to the constructor and execute the constructor//assign this object to the object LiuConsole.log (Liu.getname ());//LCYPerson.prototype.age = 22; Console.log (liu.age);// ALiu.age = 24; Console.log (liu.age);// -DeleteLiu.age;console.log (liu.age);// A

Create an employee class employee, and let it inherit the Name,sex property in person has the Get method:

functionEmployee (Name,sex,employeeid) { This. name=name;  This. sex=sex;  This. employeeid=EmployeeID;}//point the employee's prototype to an instance of personEmployee.prototype=NewPerson ();
Employee.prototype.constructor=employee; Employee.prototype.getEmployeeId=function(){return This. EmployeeID;};varchen=NewEmployee ("Chen", "female", 001); Console.log (Chen.getname ());

JavaScript Zhongyuan and Inheritance (simple example)

Related Article

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.