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)