JavaScript design mode-constructor (constructor) mode

Source: Internet
Author: User

constructor is a special method used to initialize a newly created object in cases where memory has been allocated to the object. The object constructor is used to create objects of a specific type – prepare the object for use, and the colleague receive constructor can use parameters to set member properties and method values when the object is first created.

Object Creation
There are two ways to innovate new objects, usually in javascript:

    1. The method of object face measure
      var newObj = {};

    2. A concise approach to constructors
      var newObj = new Object ();
      When the object constructor creates a wrapper for a specific value or does not pass a value, it creates a Ken object and returns

Methods for assigning values to objects:

    1. "Point" method
//设置属性‘LanFeng‘;//获取值var user= newObj.name;
    1. The Bracket method
//设置属性newObj["name"‘LanFeng‘;//获取值var user= newObj["name"];
    1. Object.defineproperty (for ECMAScript5)
//设置属性Object.defineProperty(newObj,"name",{    value:"LanFeng",    writable:true,    enumerable:true,    configurable:true})
    1. Object.defineproperties
//设置属性 Object.defineProperties(newObj,{     "someKey":{        value:"Hello Js",         writable:true    },    "anotherKey":{        value:"Foo bar",        writable:false    }})

JavaScript does not support the concept of a class, but it does support special constructor functions that are used with objects by telling JS to instantiate a new object as if it were a constructor by adding the new keyword in front of the constructor, and the object member is defined by the function.
Within the constructor, the keyword this references 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 +" have done"+ This. Miles +"Miles"; }}//Create instanced objectsvarCivio =NewCar ("Honda Civio", the,20000);varmondeo=NewCar ("Ford Mondeo", the, the);

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

There is a prototype property in JavaScript, and after invoking 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 functionCar.prototype.toString = function(){    return  This. Model +" have done"+ This. Miles +"Miles"; }//Create instanced objectsvarCivio =NewCar ("Honda Civio", the,20000);varmondeo=NewCar ("Ford Mondeo", the, the); Console.log (Civio.tostring ()) Console.log (mondeo.tostring ())

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

JavaScript design mode-constructor (constructor) mode

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.