Several modes of creating objects in JavaScript

Source: Internet
Author: User

Read the JS advanced programming of the sixth chapter, a lot of knowledge. There are several modes of creating objects, which are recorded here.

1. Factory mode

1 functionCreateperson (name, age, job) {2     varo =NewObject ();3O.name =name;4O.age =Age ;5O.job =job;6O.sayname =function() {7         return  This. Name;8     };9     returno;Ten}

There is no problem solving object recognition, you cannot know the type of an object

2. Constructor mode

functionPerson (name, age, job) { This. Name =name;  This. Age =Age ;  This. Job =job;  This. Sayname =function() {alert ( This. Name); };}varPerson1 =NewPerson ("Nicholas", "Software Engineer");varPerson2 =NewPerson ("Greg", "Doctor"); alert (Person1.constructor==Person ); alert (Person2.constructor==Person ); alert (Person1instanceofObject); alert (Person1instanceofPerson ); alert (Person2instanceofObject); alert (Person2instanceofPerson );//use as a constructor functionvarperson =NewPerson ("Nicholas", "Software Engineer");p erson.sayname ();//use as a normal functionPerson ("Greg", "Doctor"); Window.sayname ();//called in the scope of another objectvarn \NewObject (); Person.call (O,"Kristen", "Nurse"); O.sayname ();

(1) Creating an object with the new operator calling the native constructor pattern;
(2) Given an instance of a particular type;
(3) can be used to create objects, but also as ordinary functions.

3. Prototype mode

functionPerson () {}person.prototype.name= "Nicholas"; Person.prototype.age= 29; Person.prototype.job= "Software Engineer"; Person.prototype.sayName=function() {alert ( This. name);};varPerson1 =NewPerson ();p erson1.sayname ();varPerson2 =NewPerson ();p erson2.sayname (); Alert (Person1.sayname==person2.sayname);//alert (Person.prototype.isPrototypeOf (Person1));//alert (Person.prototype.isPrototypeOf (Person2));alert (object.getprototypeof (Person1)==person.prototype); alert (object.getprototypeof (person1). name);functionHasprototypeproperty (object, name) {return!hasownproperty (name) && (nameinchobject);}varperson =NewPerson (); Alert (Hasprototypeproperty (person,"Name"));p erson.name= "Greg"; alert (Hasprototypeproperty (person,"Name"));varKeys =object.getownpropertynames (Person.prototype); alert (keys);//Simpler prototype syntax: At this point the constructor property no longer points to personfunctionPerson () {}; Person.prototype={name:"Nicholas", Age:29, Job:"Software Engineer", Sayname:function() {alert ( This. Name); }};varFriend =NewPerson (); Person.prototype.sayHi=function() {alert ("Hi");}; Friend.sayhi ();functionPerson () {}; Person.prototype={constructor:person, Name:"Nicholas", Age:29, Job:"Software Engineer", friends: ["Shelby", "Court"], Sayname:function() {alert ( This. Name); }};varPerson1 =NewPerson ();varPerson2 =NewPerson ();p Erson1.friends.push ("Van"alert (person1.friends); alert ( person2.friends); alert (Person1.friends= = = Person2.friends);

(1) Understanding the prototype object is the focus, the constructor in the prototype attribute point to the prototype, the prototype constructor attribute and point to the constructor, the prototype attribute in the instance also points to the prototype, is the pointer is not a copy;
(2) using a simpler prototype syntax is equivalent to rewriting the prototype, at which point the constructor property needs to display the setting to the constructor;
(3) For reference types, one instance to the prototype or delete attribute affects other instances.

4. Constructor mode and prototype mode combination/Dynamic prototype mode

Using a combination of constructor patterns and prototype patterns, constructors define instance properties, and prototypes define shared properties and methods. The dynamic prototype pattern is based on the prototype pattern, and the if is used to determine whether the attribute or method needs to be defined in the prototype.

//Dynamic Prototyping ModefunctionPerson (name, age, job) {//Properties     This. Name =name;  This. Age =Age ;  This. Job =job;  This. Friends = ["Shelby", "Court"]; //Method    if(typeof  This. sayname! = "function") {Person.prototype.sayName=function() {alert ( This. Name);    }; }}person.prototype={Constructor:person, sayname:function() {alert ( This. Name); }}varPerson1 =NewPerson ("Nicholas", "Software Engineer");varPerson2 =NewPerson ("Greg", "Doctor");p Erson1.friends.push ("Van"alert (person1.friends); alert ( person2.friends); alert (Person1.friends===person2.friends); alert (Person1.sayname= = = Person2.sayname);

(1) The most popular is the combination mode

5. Parasitic constructor mode

/*Parasitic structural function model*/functionPerson (name, age, job) {varo =NewObject (); O.name=name; O.age=Age ; O.job=job; O.sayname=function() {alert ( This. Name);    }; returno;}varFriend =NewPerson ("Nicholas", "Software Engineer"); Friend.sayname ();functionSpecialarray () {varValues =NewArray ();    Values.push.apply (values, arguments); Values.topipedstring=function() {        return  This. Join ("|");    }; returnvalues;}varcolors =NewSpecialarray ("Red", "Blue", "green"); alert (colors.topipedstring ());

(1) objects used to customize their own special functions using native objects;
(2) Under the same conditions, it is not recommended.

6. Secure constructor Mode

(1) Only methods within the constructor can access private members, others cannot access them, and (2) This method is more secure, but cannot be used with the instanceof operator.

Several modes of creating objects in JavaScript

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.