A constructor is a special method for initializing a newly created object if memory has been allocated to it. Object constructor is used to create objects of a specific type. Prepare objects for use. Colleagues receive constructors can use parameters to set member properties and method values when the object is created for the first time.
Object creation
Innovate new objects, there are usually two ways to do it in JavaScript:
1. Object face-to-face measurement method
var newObj = {};
2. A simple method of constructor
var newObj = new Object();
When the Object constructor creates an object encapsulation for a specific value, or when no value is passed, it creates a Ken object and returns
Methods for object assignment:
1. "Point" method
//Set the attribute newObj.name = 'LanFeng';//Get the value var user= newObj.name;
1. Bracket method
//Set the attribute newObj["name"]= 'LanFeng';//Get the value var user= newObj["name"];
1.Object.defineProperty (Applicable to ECMAScript5)
//Set the property Object.defineProperty(newObj,"name",{ value:"LanFeng", writable:true, enumerable:true, configurable:true})1.Object.defineProperties
//Set the 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 used with objects. By preceding the constructor, it tells js to instantiate a new object like using a constructor, and the object members are defined by the function.
Within the constructor, the keyword this refers to the newly created object. Review object creation, basic constructor:
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 instantiated object var civio = new Car("Honda Civio",2009,20000);var mondeo= new Car("Ford Mondeo",2009,5000);The above example is a simple constructor pattern version, but it does have some problems, one of which is that it becomes difficult to use inheritance, and another is that functions like toString() are redefined separately for each new object created using the Car constructor, which is not ideal, because such functions should be shared directly on all Car type instances.
There is a prototype attribute in JavaScript. After calling the js constructor to create an object, the new object will have all the attributes of the constructor prototype. In this way, multiple objects can be created and the same prototype can be accessed to implement method sharing.
function Car(model,year,miles){ this.model = model; this.year = year; this.miles = miles;}//Prototype function Car.prototype.toString = function(){ return this.model + "has done" + this.miles +"miles"; }//Create 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 between all Cars.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.