A pair of joy enemy _javascript techniques in JavaScript constructor and Instanceof,jsoo

Source: Internet
Author: User
At least every programmer trying to javascriptoo spends a lot of effort on the object-oriented simulation rather than the business itself.
This is hard to imagine for java,c++ and even PHP developers.
What's even worse is that mock Oo has an evil attraction for JavaScript's advanced programmers.
Because doing this thing above the business, a kind of create a new programming language of the general pleasure, can make the IQ freely.
As in previous years, everyone wanted to write the common.js of a website as a framework. Until the strong launch of the Yui,jquery and so on has only slightly subsided.
However, although all frameworks have javascriptoo simulations, it is not yet time for anyone who can have a bucket of paste.
Perhaps the river is not the need of the Overlord to appear, or everyone just wait until the js2.0+ is good.
If new is an Object-oriented object, it's obvious that JavaScript is pretty good in this area.
Copy Code code 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 in harmony.
The constructor of an object is just like the literal meaning of the person who constructs it.
An object is an instance of the person that constructs it (instance).
All objects are instances of object, like Java.
JavaScript provides a prototype (prototype) way to implement method extension and inheritance
Copy Code code as follows:

Person.prototype.getName = function () {
Return this.name
};

This defines a GetName method for all objects.
Of course, you can also write in the object construct
Copy Code code as follows:

function person (name) {
THIS.name = name;
This.getname = function () {
return this.name;
};
}

But this is not only a flaw in additional performance losses, but also a privilege to access private variables.
There are other differences between it and the use of prototype, but this is not the focus of this article.
Next, we think of inheritance, a common form of writing is this.
Copy Code code 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, inherited the GetName method;
Examine the instanceof
Copy Code code as follows:

Alert (piupiu instanceof Stuff = = true);
Alert (piupiu instanceof person = = true);

Very well, it shows that stuff and person are related. Piupiu is an example of both of them, very java.
Next, we'll examine the constructor.
Copy Code code as follows:

Alert (Piupiu.constructor = = Stuff);//false
Test (Piupiu.constructor = = person);//true

The problem arises obviously new stuff why constructor is a person,
Here the truth is also chopping, we have to remember the conclusion
Conclusion: The constructor property of an object does not point to its constructor, but to the constructor property of its constructor's prototype property
Writing skills too bad, I read all feel not clear
Put it here:
The constructor property of an object Piupiu points to the constructor property of its constructor stuff's prototype property
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 they are defined
Copy Code code as follows:

function Student (name) {
THIS.name = name;
}
Student.prototype = {
Getname:function () {
return this.name;
},
Setname:function (name) {
THIS.name = name;
}
}

If the method is more, it is often written in this way, it seems to behave.
In fact, this kind of writing also sacrificed constructor
Copy Code code as follows:

var moen = new Student ("Moen");
Alert (Moen.constructor = = Student);//false
Alert (Moen.constructor = = = Object);//true

Because the {} is equivalent to the new Object (), the constructor is changed according to the above conclusion prototype={}.
When defending constructor! inheritance, we use a loop to CopyTo subclasses of the methods of the parent class
Copy Code code 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! we lost our parent-child relationship when we excitedly inherited the methods of our parents.
Copy Code code as follows:

Alert (piupiu1 instanceof Stuff1 = = true);//true
Alert (piupiu1 instanceof person = = true);//false

Obviously, we did not say that Stuff1 is inherited to person, only a for loop can explain what.
This is like a pair of contradictions. This will be lost.
So le, deep use of the object, even if you accidentally, will let Instantceof and constructor lost literal meaning.
So, when you use a frame, if you provide an inheritance function, if you don't try, you don't know which one you gave up in order to inherit.
So, when you simulate OO, you have to provide an alternative to these two literal attributes or methods of implementation, and must let the user know!
Impersonation Oo is absolutely not the problem, calling the parent class's method of the same name in a subclass method, a common feature of the OO language,
JavaScript is also very difficult to implement, the Masters have their own set of basic, but some of the intention to do it in comparison with the more natural, such as the base library.
Of course, can abandon the use of instanceof and constructor, when you do not know that there is such a thing, the work can continue, not dead people.
But if you are not fighting single-handedly, then inform your partner also abandon this pair of friends.
Similar to today's, the veteran are clear, but if not everyone in the team have a unified understanding, it is a hidden danger.
A playful phenomenon <<javascript advanced programming >> emphasis on instanceof, while << proficient javascript>> Emphasis on constructor. And each of them to another one or no mention. 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.