JavaScript constructor Mandatory invocation experience Summary _javascript tips

Source: Internet
Author: User
The following constructor is defined with gusto:
Copy Code code as follows:

var coder = function (Nick) {
This.nick = Nick;
};

What happens after the constructor is defined? Yes, hastened to instantiate:
var coder = coder (' Casper ');
What's the name of this coder brother? Quickly print down:
Copy Code code as follows:

Console.log (Coder.nick); Undefined
= =b unexpectedly is undefined!! Looking back at the instantiated statement, it's not hard to find out where the problem is: A new one is missing
var coder = coder (' Casper '); Called as a normal function, so the internal this pointer actually points to the Window object
Console.log (Window.nick); Output: Casper
var coder = new Coder (' Casper '); Plus new, everything is different, this correctly points to the currently created instance
Console.log (Coder.nick); Output: Casper

About this pointer to the problem is not discussed in this article, you can refer to the following Rhino book related chapters
Such a mistake seems very low-level, but the probability of a very high, swollen to avoid or reduce the occurrence of this situation?
You can move your hands and feet inside the inner implementation:
Copy Code code as follows:

var coder = function (Nick) {
if (!) ( This instanceof coder)) {
return new Coder (Nick);
}
This.nick = Nick;
};

In fact very simple, when instantiated, internal judgments, the current this point to the type of object, if not the type of the current constructor, force the callback constructor again.
Suddenly feel coder this name is not brim? Want to use hacker, OK, I change ... Count, a total of three to change, this is not scientific, there is no way to only change the name of the constructor on the line?
Of course:
Copy Code code as follows:

var coder = function (Nick) {
if (!) ( This instanceof Arguments.callee)) {
return new Arguments.callee (Nick);
}
This.nick = Nick;
};

Tips: It is said that in the ES 5 strict mode below Arguments.callee will be disabled, but only when the ES 5 universal also you specify to use strict mode, or you can use the divergent thinking: in JQ inside Pauta Invincible $, we all know it will return a jquery object, As follows:
var jobject = $ (' #node_id ');
There's no new! here. You should have guessed what's going on. The principle is similar, but inside the realization is much more complicated, have the time to JQ inside the realization unplug write down the summary
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.