| <script> </ script> tags: | Category: |
Today, the company completed the task in the process, some information poor. This found the following objects on the definition of js, the very comprehensive.
JS defined category there are a variety of ways:
1, the factory methods
function Car () (
var ōcar = new Object;
ocar.color = "blue";
ocar.doors = 4;
ocar.showColor = function () (
document.write (this.color)
);
return ocar;
)
var car1 = Car ();
var car2 = Car ();
Calling this function will create a new object and give it all the attributes and methods. Use this function you can create two targets of the same attributes. Of course, I-mei can pass parameters to it to change this approach.
function Car (color, door) (
var ōcar = new Object;
ocar.color = color;
ocar.doors = door;
ocar.showColor = function () (
document.write (this.color)
);
return ocar;
)
var car1 = Car ( "red", 4);
var car2 = Car ( "blue", 4);
car1.showColor () / / output: "red"
car2.showColor () / / output: "blue"
Can now pass through to the function of different parameters to get the object with different values.
In the previous example, each call function Car (), should create showcolor (), means that each object has its own showcolor () method.
But in fact, fighting each object to share the same function.
Although it can be outside the definition of the function method, and then by the properties of function point to the method.
function showColor () (
alert (this.color);
)
function Car () (
var ōcar = new Object ();
ocar.color = color;
ocar.doors = door;
ocar.showColor = showColor;
return ocar;
)
But this does not look like that function.
2, constructor methods
Constructor methods as simple as with the factory way, as follows:
function Car (color, door) (
this.color = color;
this.doors = door;
this.showColor = function () (
alert (this.color)
);
)
var car1 = new Car ( "red", 4);
var car2 = new Car ( "blue", 4);
Can be seen in the constructor does not create an internal function object is used this keyword. Constructor in the call when the object has been created, and in this function can only be used internally to access object properties.
Now used to create a new object, then the thing looks like! But it means the same as the factory. Each call will be targeted to create their own methods.
3, a prototype approach
The use of a prototype object attributes. First of all functions used to create the class name space, and then all of the properties and methods have been given prototype attributes.
function Car () (
)
Car.prototype.color = "red";
Car.prototype.doors = 4;
Car.prototype.showColor = function () (
alert (this.color);
)
var car1 = new Car ();
var car2 = new Car ();
In this code, first of all, the definition of a function space, and then through the prototype property to define the attributes of the object. Call this function, the prototype of all of the properties will be immediately given to the creation of the object, the object of all the functions are stored in point showColor () pointer, grammar-wise, it seems all belong to the same object.
However, there is no parameter of this function, can not pass parameters to initialize the attributes of the object must be created after the change in the default value attributes.
Prototype has a very serious way the question is when the point is that the object attributes, such as an array.
function Car () (
)
Car.prototype.color = "red";
Car.prototype.doors = 4;
Car.prototype.arr = new Array ( "a", "b");
Car.prototype.showColor = function () (
alert (this.color);
)
var car1 = new Car ();
var car2 = new Car ();
car1.arr.push ( "cc");
alert (car1.arr); / / output: aa, bb, cc
alert (car2.arr); / / output: aa, bb, cc
Here because of the reference value of array, Car of the two objects are the same point to an array, so as to add value in the car1 after car2 can see.
4, a mixture of constructor / prototype approach
Joint is constructor / prototype methods can be like any other programming language to create the same object, is the definition of the object constructor function of the non-attribute, defined by a prototype object.
function Car (color, door) (
this.color = color;
this.doors = door;
this.arr = new Array ( "aa", "bb");
)
Car.prototype.showColor () (
alert (this.color);
)
var car1 = new Car ( "red", 4);
var car2 = new Car ( "blue", 4);
car1.arr.push ( "cc");
alert (car1.arr); / / output: aa, bb, cc
alert (car2.arr); / / output: aa, bb
5, dynamic prototype approach
Dynamic way the prototype of the constructor with the mixed / prototype manner similar principle. The only difference is that given the location of the object methods.
function Car (color, door) (
this.color = color;
this.doors = door;
this.arr = new Array ( "aa", "bb");
if (typeof Car._initialized == "undefined") (
Car.prototype.showColor = function () (
alert (this.color);
);
Car._initialized = true;
)
)
Dynamic approach is to use the prototype to determine whether a sign has been given to the prototype method. This will ensure that the only way to create a
6, mixed-plant approach
It aims to create a false division constructor, only to return a new instance of the object.
function Car () (
var ōcar = new Object ();
ocar.color = "red";
ocar.doors = 4;
ocar.showColor = function () (
alert (this.color)
);
return ocar;
)
The manner in which the plant is different, the use of new operators.
These are all methods to create objects. At present, the most widely used is the mixed-constructor / prototype approach, in addition, dynamic mode is also very popular prototype. Functions and constructor / prototype equivalent manner.







