Effective JavaScript Item 33 lets constructors no longer rely on the new keyword

Source: Internet
Author: User

This series as Effectivejavascript's reading notes.

in the function when used as a constructor, you need to make sure that the function is New keyword is called.


function User (name, passwordhash) {this.name = Name;this.passwordhash = PasswordHash;}

if the above constructor is called, you forget to use the New keyword, then:


var u = User ("Baravelli", "d8b74df393528d51cd19980ae0aa028e"); u; Undefinedthis.name; "Baravelli" This.passwordhash; "d8b74df393528d51cd19980ae0aa028e"

can be found. u is a undefined , while this.name as well This.passwordhash is assigned a value. But here the this Point is the global object.

If you declare the constructor to be dependent on the Strict mode:


function User (name, PasswordHash) {"Use strict"; this.name = Name;this.passwordhash = PasswordHash;} var u = User ("Baravelli", "d8b74df393528d51cd19980ae0aa028e");//Error:this is undefined

then forget to use New keyword, when you call the this.name= name the time will be thrown TypeError error. This is because in strict mode, The default point of this is set to undefined instead of the global object.

So, is there a way to ensure that when a function is called, regardless of the New keyword or not, the function can be used as a constructor function? The following code is an implementation that uses the instanceof operation:


function User (name, PasswordHash) {if (!) ( This instanceof user) {return new user (name, passwordhash);} THIS.name = Name;this.passwordhash = PasswordHash;} var x = user ("Baravelli", "d8b74df393528d51cd19980ae0aa028e"), var y = new User ("Baravelli", " d8b74df393528d51cd19980ae0aa028e "); x instanceof User; Truey instanceof User; True

above the if code blocks are used to handle unused New In the case of a call. When new is not used , the Point of this is not an instance of the User , but when new is used keyword, This the point is a User the instance of the type.

the other is more suitable for ES5 the implementation used in the environment is as follows:


function User (name, passwordhash) {var = this instanceof User? This:Object.create (user.prototype); self.name = Name ; self.passwordhash = Passwordhash;return self;}

object.create method is ES5 provides a method that can accept an object as a newly created object. prototype . In a non- ES5 Environment, it is necessary to implement a object.create method First:


if (typeof object.create = = = "undefined") {object.create = function (prototype) {function C () {}c.prototype = Prototype;re Turn new C ();};}

in fact, object.create The method also has a version that accepts the second argument, and the second parameter represents a series of attributes that are given on the newly created object.

when the above function does use the New when the call is made, it is also possible to get the new object returned correctly. This is due to the constructor overlay mode (Constructor override pattern). The implication of this pattern is that the return value of an expression that uses the new keyword can be overridden by an explicit return . As in the code above , returnself is used to explicitly define the return value.

Of Course, the above work is not necessary in some cases. However, when a function needs to be called as a constructor, it must be described, and using a document is a way of using the first letter of the name of the function ( based on Some of the conventional JavaScript languages ).

Summarize:

    1. Use object.create to ensure that a function is actually called as a constructor, whether New whether the keyword is used.
    2. For functions that are constructors, this is indicated in the documentation to ensure that other users can use it correctly.

Effective JavaScript Item 33 lets constructors no longer rely on the new keyword

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.