JavaScript I learn six objects factory functions and constructors

Source: Internet
Author: User

This article is Jin Xu-liang teacher NetEase Cloud Class of course notes, record down, for memo.

Overview
    • Using object literals, or adding new members dynamically to an empty object, is the easiest way to create an object.
    • However, in addition to these two commonly used object creation methods, JavaScript also provides other ways to create objects.
1). Create an object using the factory function

We can write a function whose function is to create an object that can be called an object factory method.

1    //Factory Functions2         functionCreateperson (name, age, job) {3             varo =NewObject ();4O.name =name;5O.age =Age ;6O.job =job;7O.sayname =function () {8Console.info ( This. Name);9             };Ten             returno; One         } A         //create an object using the Factory function -         varPerson1 = Createperson (' Zhang San ', 29, ' software engineer '); -         varPerson2 = Createperson (' John Doe ', 40, ' doctor ');

2). Define object constructor a). Object construction function First Capital B). Use the This keyword internally to add member C to an object. Invoking an object constructor with the New keyword
       //Define object "construct" function        functionPerson (name, age, job) { This. Name =name;  This. Age =Age ;  This. Job =job;  This. Sayname =function() {Console.info ( This. Name);        }; }        //To create an object using the new Call object constructor        varP1 =NewPerson (' Zhang San ', 29, ' software engineer '); varP2 =NewPerson (' John Doe ', 40, ' doctor ');

"Constructors" that are called in the normal way

The constructor is actually a function, except that it must be called with a "new" keyword, and if the keyword is not added, the call to it is considered to be a normal function call.

1        // as a constructor for a normal function call, the property that is added through this 2         // becomes the property and method of the Window object.  3         console.info (window.name);   Zhang San 4         //5         / / software engineer 

The object constructor looks like this:

1 function Person (name) {2      this. Name = name; 3       This function () {4          return  This . Name; 5      }; 6 }

This is actually the case (signaling):

1 functionPerson (name) {2       //var this = {};3        This. Name =name;4        This. Say =function () {5            return"I am" + This. Name;6      };7       //return this;8}
work done by the constructor1. Create a new object 2. Let the constructor of this reference this newly created object 3. Executes the code in the constructor, which typically completes the task of adding properties to the new Object 4. Returns the newly created object reference to the outside world. The difference between an object constructor and an object factory method 1. Object constructors do not have an explicit object creation code 2. The properties and methods that the new object should have are added by the this reference. 3. There is no return statement in the object constructor that usually sets the first letter of the object constructor to uppercase to distinguish it from the normal function. The constructor property of the object

a). Use the object factory function to create an object with the constructor property of each object referencing object ()

1       var person = Createperson (' Zhang San ', 29, ' software engineer '); 2 3      // use the factory method to create an object whose constructor property references the object () function 4         Console.info (Person1.constructor = = = Object);  // true

b). Create an object using the object constructor, and the constructor property of each object references this constructor

1 var New Person (' Zhang San ', 29, ' software engineer '); 2 // create an object using the object constructor, 3 // The constructor property of each object, referencing this constructor 4 // true

How to avoid "forget" new?

You can use Arguments.callee to solve this problem

1         //understand the role of Arguments.callee2         functionTestargumentscallee () {3Console.info ( This);4Console.info ( This instanceofTestargumentscallee);5Console.info ( This instanceofArguments.callee);6         };7Testargumentscallee ();//window8                                             //false9                                             //falseTen         NewTestargumentscallee ();//Testargumentscallee One                                                      //true A                                                      //true

So, you can use Arguments.callee directly

1         //Avoid forgetting new2         functionMyObject (value) {3             if(! ( This instanceofArguments.callee)) {4                 //if the caller forgets to add new, add new to call again5                 return NewMyObject (value);6             }7              This. Prop =value;8         }9         //TestTen         varObj1 =NewMyObject (100); OneConsole.info (Obj1.prop);// - A         varObj2 = MyObject (200); -Console.info (Obj2.prop);// $

JavaScript I learn six objects factory functions and constructors

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.