JS Object-oriented object creation

Source: Internet
Author: User

Factory mode
function Createperson (name,age,job) {varo =NewObject (); O.name=name; O.age=Age ; O.job=job; O.sayname=function () {alert ( This. Name); }    returno;}varPerson1 = Createperson ('zy1', -,'New worker');varPerson2 = Createperson ('Zy2', in,'New Worker2');

Factory mode solves the problem of creating multiple similar objects, but does not solve the object recognition problem (that is, how to know the type of an object). Because all are object, unlike date, array, and so on, there is a constructor pattern.

Constructor mode
function Person (name,age,job) { This. Name =name;  This. Age =Age ;  This. Job =job;  This. Sayname =function () {alert ( This. Name); }}varPerson1 =NewPerson ('zy1', -,'New worker');varPerson2 =NewPerson ('Zy2', in,'New Worker2');

There are several differences between the constructor pattern and the Factory mode

    • Object not explicitly created
    • Assigning properties and methods directly to the This object
    • No return statement

The object created by this method has a constructor property, which in this case points to person, person1.constructor = = person


The difference between a constructor and a normal function is the way it is called, which is new. If it doesn't work with new, it's nothing like a normal function.

Disadvantages of the constructor:

Each method in the constructor is recreated on each instance, if the Person1 and Person2 methods above are Sayname (), not the same instance of a function. Because a function is an object, that is, each definition of a function, that is, instantiating an object.

That is, the above equivalent this.sayname = new Function ("Alert (this.name)")


The above method can also be changed to

function Person (name,age,job) {    this. Name = name;      this. Age = Age ;     this. Job = job;     this. sayname = sayname}function sayname () {    alert (this. Name)}

But this sayname () becomes a global function, which is not good, so it leads to the following pattern

Prototype mode

The concrete view of the prototype, http://www.cnblogs.com/myzy/p/6083141.html, is not explained here.

The advantage of using prototypes is that you can have all object instances share the properties and methods that it contains.

' Zy '  ; Person.prototype.age='+'= function () {   alert (this   . Name); }varnew person (); var New  //true

Simpler prototype syntax

= {    name:'zy',    Contructor:person,age    :29  ,    sayname:function () {        alert (this. Name);    }}

Note that the contructor is reset here. Because if this is not added Contructor, the constructor will no longer point to the person in such a way as to create a new object

Disadvantage: If an instance modifies a property value in the prototype, the value of this property in the other instance also changes.

combining the constructor pattern with the prototype pattern
function Person (name,age,job) {    this. Name = name;      this. Age = Age ;     this. friends=['a','b'= {    Contructor:person,    sayname:function () {        alert (this. Name);    }}

This pattern is the most widely used method.

Dynamic Prototyping Mode
 function person (name,age,job) { this     . Name = name;     this . Age = age;  this . Friends=[ " a  " ,  " b  "  ];  if  (typeof  this . Sayname! =  " function   "  this     .name); }}

Where the IF statement can be any property or method that should exist after initialization, you do not have to examine each statement and each method with a large heap of if statements, just check one of them.

Parasitic constructor mode
 function person (name,age,job) { var  o    = new   Object ();    O.name  = name;    O.age  = age;    O.job  = job; O.sayname  = function () {alert ( this     .name);  return   o;}  var  person1 = new  person ("  zy1   ", 28 , "  new worker  Span style= "COLOR: #800000" ""); 

With the new operator and the wrapper function called the constructor, this pattern is exactly the same as the factory pattern.

Secure constructor Mode
function Person (name,age,job) {    var o  new  Object ();     = function () {        alert (name);    }     return o;} var person1 = person ('zy1','new worker '  ');

This method does not refer to this,new. This mode provides this security, making it ideal for certain secure execution environments.

JS Object-oriented object creation

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.