[Experience Summary] force call of Constructor

Source: Internet
Author: User

Happily defined the following constructor:

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:

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 oneNew

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: caspervar coder = new coder ('casper '); // when new is added, everything is different. This correctly points to the console of the currently created instance. log (coder. nick); // output: Casper

 

AboutThisThe Pointer Points are 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:

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

 

It is actually very simple. During the instantiation, the internal judgment is as follows:ThisThe type of the object to be directed to. If the type of the current constructor is not the type of the constructor, the constructor must 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:

var Coder = function( nick ){    if( !(this instanceof arguments.callee) ){        return new arguments.callee( nick );    }       this.nick = nick;};

 

AboutArguments. callee, See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/callee

TIPS: It is said that arguments. callee will be disabled in the strict mode of es 5. However, if elasticsearch 5 is popular only, you must specify the strict mode. Otherwise, it can still be used.

 

Divergent thinking: in JQ, We can package the invincible $ in the world. Everyone knows that it will return a jquery object, as shown below:

var jObject = $('#node_id');

 

Have you found that there is no such thing?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.

Over

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.