Functions in JavaScript are omnipotent, and in addition to the function definitions used, they can be used for the definition of a class. JavaScript inheritance, it is also a bit strange to say, there is no public,private such as access control decoration, there is no implement or other specific symbols to illustrate is the implementation of inheritance. Refer to the following example for the inheritance of JavaScript classes. Dancheng Shu Marine Environmental protection
01 |
<script type= "text/javascript" > |
04 |
this .Gender = "female" ; |
06 |
this .Words = "Silence" ; |
08 |
this .shouting = function () { |
13 |
function Programmer() { |
16 |
Programmer.prototype = new Person; |
18 |
Programmer.prototype.typeCode = function () { |
19 |
alert( "俺是敲代码的!IT民工,很不开心。子类的方法" ); |
23 |
var a = new Programmer(); |
24 |
alert(a.Gender); // 调用父类的属性 |
25 |
a.shouting(); // 调用父类的方法 |
26 |
a.typeCode(); // 调用子类的方法 |
Simulating classes and inheritance in JavaScript
In object-oriented languages, we use classes to create a custom object. While everything in JavaScript is an object, what is the way to create a custom object?
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 the template (prototype) (actually not a copy but a link, but the link is not visible, It feels like a copy to the people.
Let's take a look at an example of creating a custom object through prototype:
02 |
function Person(name, sex) { |
06 |
// 定义Person的原型,原型中的属性可以被自定义对象引用 |
Here we refer to the function person as the constructor, which is the function that creates the custom object. As you can see, JavaScript simulates the functionality of a class by constructing functions and prototypes.
Code to create a custom object (instantiation Class):
1 |
var zhang = new Person( "ZhangSan" , "man" ); |
2 |
console.log(zhang.getName()); // "ZhangSan" |
3 |
var chun = new Person( "ChunHua" , "woman" ); |
4 |
console.log(chun.getName()); // "ChunHua" |
When code var Zhang = new person ("Zhangsan", "man") executes, there are actually several things that are done internally:
- Creates a blank object (new Object ()).
- Copy the attributes in Person.prototype (key-value pairs) into this empty object (as we mentioned earlier, the internal implementation is not a copy but a hidden link).
- Pass the object through the This keyword to the constructor and execute the constructor.
- Assign this object to the variable Zhang.
To prove that the prototype template is not copied to the instantiated object, but rather a link, see the following code:
01 |
function Person(name, sex) { |
05 |
Person.prototype.age = 20; |
06 |
var zhang = new Person( "ZhangSan" , "man" ); |
07 |
console.log(zhang.age); // 20 |
10 |
console.log(zhang.age); // 19 |
12 |
// 在删除实例属性age后,此属性值又从prototype中获取 |
13 |
console.log(zhang.age); // 20 |
This hidden prototype link, implemented within JavaScript, is the moist soil that JavaScript relies on to survive, and is the basis for the simulation of inheritance.
How do I implement simple inheritance in JavaScript?
The following example creates an employee class employee, which inherits all the properties from the prototype prototype from person.
01 |
function Employee(name, sex, employeeID) { |
04 |
this .employeeID = employeeID; |
06 |
// 将Employee的原型指向Person的一个实例 |
07 |
// 因为Person的实例可以调用Person原型中的方法, 所以Employee的实例也可以调用Person原型中的所有属性。 |
08 |
Employee.prototype = new Person(); |
09 |
Employee.prototype.getEmployeeID = function () { |
10 |
return this .employeeID; |
12 |
var zhang = new Employee( "ZhangSan" , "man" , "1234" ); |
13 |
console.log(zhang.getName()); // "ZhangSan |
The above implementation of inheritance is coarse, and there are many problems:
- It is not appropriate to instantiate a person when creating an employee constructor and prototype (hereafter referred to as a class).
- The constructor of the employee cannot invoke the constructor of the parent class person, resulting in a duplicate assignment of the name and sex attributes in the employee constructor.
- The function in the employee overrides the function with the same name in person, without overloading the mechanism (and the previous one is a type of problem).
- The syntax for creating JavaScript classes is too fragmented to be as elegant as the syntax in C#/java.
- The implementation has a pointing error in the constructor property.
How JavaScript implements Inheritance