JavaScript design Pattern Constructor (constructor) pattern _javascript techniques

Source: Internet
Author: User

constructor is a special way to initialize a newly created object when memory has been allocated to the object. The object constructor is used to create objects of a particular type – ready objects for use, and the colleague receive constructor can use parameters to set member properties and method values the first time an object is created.

Object creation
There are usually two ways to create new objects in javascript:
1. Object-Facing Volume method
var newObj = {};

2. The concise method of the builder
var newObj = new Object ();
when the object constructor creates a package of objects for a particular value, or does not pass a value, it creates a Ken that object and returns

Methods for assigning values to objects:
1. " Point "method

 Set Property
newobj.name = ' Lanfeng ';
Gets the value
var user= newobj.name;

1. The Bracket method

 Set Property
newobj["name"]= ' Lanfeng ';
Gets
the value var user= newobj["name"];

1.object.defineproperty (applicable ECMAScript5)

 Set Property
Object.defineproperty (NEWOBJ, "name", {
  value: "Lanfeng",
  Writable:true,
  Enumerable: True,
  configurable:true
})

1.object.defineproperties

 Set Property
 object.defineproperties (newobj,{
   "Somekey": {
    value: "Hello Js",
     writable:true
  },
  ' Anotherkey ': {
    value: ' Foo bar ',
    writable:false
  }
}

JavaScript does not support the concept of classes, but it does support special constructor functions that are used with objects, by adding the new keyword in front of the constructor, telling JS to instantiate an object as if using a constructor, and the object member is defined by the function.
Within the constructor, the keyword this refers to the newly created object. Review object creation, basic builder:

function car (model,year,miles) {
  This.model = model;
  This.year = year;
  This.miles = miles;
  this.tostring = function () {return
  This.model + ' has done ' + This.miles + ' Miles ';
  }
}
Create an instantiated object

var Civio = new Car ("Honda Civio", 2009,20000);

var mondeo= new car ("Ford Mondeo", 2009,5000);

The example above is a simple constructor schema version, but it does have some problems, one of which is that it becomes difficult to use inheritance, and another problem is that a function such as toString () is redefined separately for each new object created with the car constructor, which is not ideal, Because this function should be shared directly in all instances of the car type.

JavaScript has a prototype attribute, call the JS constructor to create an object, the new object will have all the properties of the constructor prototype, in this way, you can create multiple objects, and access the same prototype, to implement method sharing.

function car (model,year,miles) {
  This.model = model;
  This.year = year;
  This.miles = miles;

}


Prototype functions
Car.prototype.toString = function () {return
  This.model + ' has done ' + This.miles + ' Miles ';
  }

Create an instantiated object

var Civio = new Car ("Honda Civio", 2009,20000);
var mondeo= new car ("Ford Mondeo", 2009,5000);
Console.log (Civio.tostring ())
Console.log (mondeo.tostring ())

Now a single instance of ToString () can be shared across all car pairs.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.