JavaScript constructor and instanceof, a happy family in JSOO

Source: Internet
Author: User

At least every programmer who tries to use JavaScriptOO spends a lot of energy on the simulation of object-oriented mechanisms rather than the business itself.
This is hard to imagine for Java, C ++, and even Php developers.
What's worse is that simulating OO has an evil attraction for senior JavaScript programmers.
This is because it is beyond the business level and has the pleasure of creating a new programming language, which allows IQ to be enjoyed.
Just as in the past few years, everyone wanted to write their own website's common. js as a framework. It was not until YUI, JQuery, and so on were launched.
However, although various frameworks have simulated JavaScriptOO, it is not yet time for anyone who can make a bucket of obfuscation.
Maybe the leader is not needed in the rivers and lakes, or you just need to wait until JS2.0 +.
If we can say that new is object-oriented, JavaScript is obviously very good in this regard.
Copy codeThe Code is as follows:
Function Person (name ){
This. name = name;
}
Var lenel = new person ("lenel ");
Alert (lenel. constructor === Person );
Alert (lenel instanceof Person = true );
Alert (lenel instanceof Object === true );

So far, everything is harmonious.
The constructor of an object literally points to the Person that constructs it.
An object is an instance that constructs its Person ).
All objects are Object instances, such as Java.
JavaScript provides prototype for method expansion and inheritance.
Copy codeThe Code is as follows:
Person. prototype. getName = function (){
Return this. name
};

This defines that all objects have the getName method.
Of course, you can also write it in the Object Construction
Copy codeThe Code is as follows:
Function Person (name ){
This. name = name;
This. getName = function (){
Return this. name;
};
}

However, this approach not only brings about extra performance loss, but also brings about the privilege to access private variables.
It is different from the prototype method, but this is not the focus of this article.
Next, we think of inheritance. This is a common method.
Copy codeThe Code is as follows:
Function Stuff (name, id ){
This. name = name;
This. id = id;
}
Stuff. prototype = new Person ();
Var piupiu = new Stuff ("piupiu", "007 ");
Alert (piupiu. getName ());

Very good, inheriting the getName method;
Evaluate the knowledge of instanceof
Copy codeThe Code is as follows:
Alert (piupiu instanceof Stuff === true );
Alert (piupiu instanceof Person = true );

Very good. It means that Stuff and Person are related. piupiu is the two instances of Stuff and is very Java.
Next, we will examine constructor.
Copy codeThe Code is as follows:
Alert (piupiu. constructor === Stuff); // false
Test (piupiu. constructor === Person); // true

When the problem arises, it is clear that the new Stuff is a constructor but a Person,
The reason here is strong words, so we have to remember the conclusion.
Conclusion: The constructor attribute of an object does not point to its constructor, but to the constructor attribute of its prototype attribute.
The text skills are too bad. I thought I did not speak clearly after reading it.
Put it here:
The constructor attribute of the object piupiu points to the constructor attribute of the prototype attribute of its construcff.
Because Stuff. prototype = new Person ();
So Stuff. prototype. constructor = Person.
So piupiu. consturctor === Person.
The above conclusions appear not only when objects are inherited, but when objects are defined
Copy codeThe Code is as follows:
Function Student (name ){
This. name = name;
}
Student. prototype = {
GetName: function (){
Return this. name;
},
SetName: function (name ){
This. name = name;
}
}

If there are many methods, it is often written in this way, and it looks more regular.
In fact, this method sacrifices constructor.
Copy codeThe Code is as follows:
Var moen = new Student ("moen ");
Alert (moen. constructor === Student); // false
Alert (moen. constructor === Object); // true

Because {} is equivalent to new Object (), constructor has changed according to prototype =.
Protect constructor! During inheritance, we use a loop to copy the parent class method to the subclass.
Copy codeThe Code is as follows:
Function Stuff1 (name, id ){
This. name = name;
This. id = id;
}
For (var fn in Person. prototype ){
Stuff1.prototype [fn] = Person. prototype [fn];
}
Var piupiu1 = new Stuff1 ("piupiu1", "008 ");
Alert (piupiu1.getName () = "piupiu1 ");
Alert (piupiu1.constructor === Stuff1 );

It works! When we are eager to inherit all the methods of the parent class, we lose the parent-child relationship.
Copy codeThe Code is as follows:
Alert (piupiu1 instanceof Stuff1 === true); // true
Alert (piupiu1 instanceof Person = true); // false

Obviously, we didn't say that Stuff1 is inherited to Person. What does a for loop mean.
This seems to be a pair of contradictions.
So when using objects in depth, even when you are not careful, it will make instantceof and constructor lose the literal meaning.
Therefore, when using the framework, if the inheritance function is provided, you will not know which one you gave up to inherit.
Therefore, when simulating OO, you must provide the Implementation of the attributes or methods that replace these two literal meanings, and make them known to the user!
Simulating OO is definitely not just a problem. In the subclass method, the method of the same name of the parent class is called, which is a common feature of the OO language,
It is also very difficult to implement JavaScript. The masters basically have their own set, but some of them are more natural than using them, such as the Base library.
Of course, you can discard the use of instanceof and constructor. If you don't know that there is such a thing, you can continue your work and you won't be able to die.
However, if you do not fight with each other, you should be notified that your Partner should abandon this enemy.
Similar to today's situation, the old birds are very clear, but if not everyone in the team has a unified understanding, it is a hidden danger.
<JavaScript advanced programming> emphasizes instanceof, and <JavaScript proficient> emphasizes constructor. however, they do not mention the other one. resentment .. very deep...

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.