Javascript constructor force call experience

Source: Internet
Author: User

Happily defined the following constructor:
Copy codeThe Code is as follows:
Var Coder = function (nick ){
This. nick = nick;
};

After the constructor is defined? That's right. instantiate it quickly:
Var coder = Coder ('casper ');
What is the name of the coder brother? Print the following:
Copy codeThe Code is as follows:
Console. log (coder. nick); // undefined
= B is undefined !! Looking back at the instantiated statement, it is not difficult to find out where the problem is: missing a new
Var coder = Coder ('casper '); // It is called as a common function, so the internal this pointer actually points to the window object
Console. log (window. nick); // output: casper
Var coder = new Coder ('casper '); // with the addition of new, everything is different. this correctly points to the currently created instance
Console. log (coder. nick); // output: casper

The point of this pointer is not discussed in this article. You can refer to the relevant chapters of the rhino book.
This kind of error seems quite low-level, but the probability of occurrence is quite high. Is it possible to avoid or reduce the occurrence of this situation?
You can move your hands and feet in the internal implementation:
Copy codeThe Code is as follows:
Var Coder = function (nick ){
If (! (This instanceof Coder )){
Return new Coder (nick );
}
This. nick = nick;
};

In fact, it is very simple. During the instantiation, We can internally determine the type of the object to which this currently points. If it is not the type of the current constructor, the constructor is forced to be called again.
Suddenly I think Coder is not a good name? I want to use Hacker... There are three changes to the number. This is not scientific. Is there a way to change the name of the constructor?
Of course:
Copy codeThe Code is as follows:
Var Coder = function (nick ){
If (! (This instanceof arguments. callee )){
Return new arguments. callee (nick );
}
This. nick = nick;
};

Tips: It is said that arguments is in the strict mode of ES 5. callee will be disabled, but only when elasticsearch 5 is popular and you specify a strict mode, otherwise you can still use the divergent thinking: in JQ, pack the world's invincible $, as we all know, it will return a jquery object, as shown below:
Var jObject = $ ('# node_id ');
No new! I guess what's going on. The principle is similar, but the implementation in it is much more complicated. If you have time, unplug the implementation in JQ and write down the summary.

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.