I think everyone has their own opinions on whether Javascript is an object-oriented language or an object-oriented language. Those faithful fans of Javascript must say that Javascript is an object-oriented language. For example, the statement about Javascript in the book "The Return of the King of Javascript" is based on prototype object-oriented. Let me talk about my personal opinion. The three object-oriented features: inheritance, polymorphism, and encapsulation. Although Javascript is not as fast as object-oriented languages such as Java and C#, it also has certain support after all. Therefore, it makes sense to say that Javascript is an object-oriented language, but from the inheritance part, there are a series of inheritance methods, but each inheritance method cannot realize the power of a truly object-oriented language. Therefore, it is far-fetched to say that it is object-oriented. In summary, my understanding of Javascript is more willing to call it a simplified object-oriented, or "pseudo" object-oriented (this pseudo-word has no derogatory meaning).
Today, let’s talk about the first feature of object-oriented: inheritance.
What is inheritance? I don't want to talk nonsense about this. There is an animal, a person, and a girl. This is the simplest and typical inheritance chain.
In object-oriented such as C#, it's easy.
The code copy is as follows:
class Animal
{ }
class People:Animal
{ }
class Girl:People
{ }
So in Javascript, there is no class or inheritance to provide implementation, what should we do?
Object disguise (construction inheritance method)
What is object camouflage? What we might call construct inheritance is easier to understand. As the name suggests, it is to use constructors to play with inheritance. In fact, it means that the parent class constructor is regarded as an ordinary method and put it into the subclass constructor to execute. In this way, when constructing an object, the subclass object can of course construct the parent class method!
Let's use the above example, the code is as follows:
The code copy is as follows:
function Animal()
{
this.Run=function(){alert("I can run");};
}
function People(name)
{
//Here, the construction method of the parent class is passed in, and then the construction method of the parent class is executed. At this time, the methods in the parent class can be used.
this.father=Animal;
this.father();
//Remember to delete it, otherwise when the subclass is added to the method with the same name as the parent class, it will be modified to the parent class.
delete this.Father;
this.name=name;
this.Say=function(){alert("My name is "+this.name);}
}
function Girl(name,age)
{
this.father=People;
this.father(name);
delete this.father;
this.age=age;
this.Introduce=function(){alert("My name is "+this.name+".I am "+this.age);};
}
In this way, an inheritance chain is implemented, and the test is:
The code copy is as follows:
var a=new Animal();
a.Run();
var p=new People("Windking");
p.Run();
p.Say();
var g=new Girl("Xuan",22);
g.Run();
g.Say();
g.Introduce();
The results are as follows:
a.
b.
c.
d.
e.
f.
The test succeeded!
Let's summarize the key of this code, specify the parent class, declare the parent class object, and then delete temporary variables. Do you think it's a bit troublesome? At least that's what I think. Once I forget to delete, I have to bear the risk of the parent class being modified. For this, we use call and apply to improve this!
Let’s look at the code, and the example above (to make it easier for everyone to understand, the need to change it, Animal has a name):
The code copy is as follows:
function Animal(name)
{
this.Run=function(){alert("I can Run");};
}
function People(name)
{
//Use call method to implement inheritance
this.father=Animal;
this.father.call(this,name);
this.name=name;
this.SayName=function(){alert("My name is "+this.name;);};
}
function Girl(name,age)
{
//Use the apply method to implement inheritance
this.father=People;
this.father.apply(this,new Array(name));
this.age=age;
this.Introduce=function(){alert("My name is "+this.name+".I am "+this.age);};
}
Using the same test code, it was found that the test was just as successful.
If you are a novice, you may feel a little dizzy when you look at the two codes below. What is call and what is apply? OK, in the special topic of Wanzhuan inheritance, I have added a supplementary issue series. If you don’t understand this, you can read my article: "Word of Wanzhuan: call and apply".
Object disguise is just a way to implement inheritance. In the next article, I will continue to write about other inheritance methods and the advantages and disadvantages of several inheritance methods. Welcome to continue to pay attention.