Javascript constructor and instanceof, a happy family in jsoo

Source: Internet
Author: User

At least each criptoo Attempt Program Members spend a lot of energy on the simulation of the object-oriented mechanism 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 this is beyond the business, and there is a kind of innovation. Programming Language The general pleasure is that IQ can 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. CopyCode The 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 ConstructionCopy 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 instanceofCopy 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 definedCopy 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 have never said that stuff1 inherits from person. What can be explained by a for loop.
This seems to be a conflict .. you must lose yourself.
therefore, when using objects in depth, even when you are not careful, instantceof and constructor will be dropped to the literal meaning.
therefore, when using the framework, if the inheritance function is provided, you will not know which one you gave up for inheritance.
therefore, when simulating Oo, you must provide the Implementation of attributes or methods that replace these two literal meanings, and you must be aware of them!
simulating Oo is definitely not just a problem. Calling the method of the same name of the parent class in the subclass method is a common feature of OO language.
JavaScript implementation is also very difficult, masters basically have their own set, but some of them are more natural to use, such as the base library.
Of course, You can discard the use of instanceof and constructor. If you do not know such a thing, you can continue your work and never 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 clear, but if not everyone in the team has a unified understanding, it is a hidden risk.
A typical phenomenon emphasizes instanceof, while 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.