Constructors that implement the no new keyword in javascript

Source: Internet
Author: User

Constructors that implement the no new keyword in javascript

In general, it is necessary to use the keyword new when creating objects in JavaScript, but at some point the developer wants the constructor to be called normally regardless of whether the new keyword is explicitly used, i.e. the constructor also functions as a simple factory. One of the features of JavaScript makes this possible: If an object is returned in the constructor, regardless of whether the new keyword is used, the value returned is the value of the function return.

Based on this characteristic, this article introduced four kinds of realization way, the Welcome shot brick ~

1. Return the object literal in the constructor

1 functionPerson (name) {2     return {3 Name:name,4GetName:function () { 5             return  This. Name;6         }7     };8 }9Console.log (NewPerson (' Ralph '). GetName ());//RalphTenConsole.log (Person (' Ralph '). GetName ());//Ralph

Disadvantages:
Inconvenient control of prototype attributes, which is not conducive to efficient extension of object methods, instanceof operator invalidation and constructor property loss

2. Constructing an object using an intrinsic function in a constructor

1 functionPerson (name) {2     //lazy Loading, which initializes the intrinsic function when the person function is first invoked _person3     if(!person.inited) {4Person._person =function(name) {5              This. Name =name;6         };7         //methods can be extended using prototype8Person._person.prototype = {9             //normal use of the constructor propertyTen Constructor:person, OneGetName:function () { A                 return  This. Name; -             } -         }; the         //You can use the instanceof operator normally -Person.prototype =Person._person.prototype; -         //Mark as initialized -person.inited =true; +     } -     return NewPerson._person (name); + } AConsole.log (NewPerson (' Ralph '). GetName ());//Ralph atConsole.log (Person (' Ralph '). GetName ());//Ralph

Disadvantages:
Coding is more complex, requires _person as an auxiliary internal constructor, and requires manual modification of properties such as prototype and constructor

3. Using the instanceof operator

1 functionPerson (name) {2     //if New,this is used to point to the newly generated person instance3     //If you call person directly without using new, this here points to the Window object4     if(! ( This instanceofPerson )) {5         return NewPerson (name);6     }7      This. Name =name;8 }9Person.prototype.getName =function () {Ten     return  This. Name; One }; AConsole.log (NewPerson (' Ralph '). GetName ());//Ralph -Console.log (Person (' Ralph '). GetName ());//Ralph

Disadvantages:
You need to specify the constructor name for the person when judging this instanceof, not enough abstraction, you need to modify the statement manually when you modify the constructor name

4. Using the callee attribute and the constructor property

1 functionPerson (name) {2     //Arguments.callee point to Person function3     //This.constructor points to the person function only in the case where new is used4     if(Arguments.callee!== This. Constructor) {5         return NewPerson (name);6     }7      This. Name =name;8 }9Person.prototype.getName =function () {Ten     return  This. Name; One }; AConsole.log (NewPerson (' Ralph '). GetName ());//Ralph -Console.log (Person (' Ralph '). GetName ());//Ralph

Disadvantages:
The callee property cannot be used in strict mode

(End of full text)

Constructors that implement the no new keyword in javascript

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.