Chapter V web front-end development engineer --JavaScript Advanced Programming 5-6 JavaScript Object Functions

                                                                               JavaScript Object Functions

 

This lesson is to talk:

  1. JavaScript object creation
  2. JavaScript Object basic operations
  3. JavaScript function object

                                                                                Speaker teachers: Head teacher

III. Function object

In order to solve the problem of multiple similar object declaration, we can use a method called the factory pattern, this method to instantiate an object is to solve a large amount of duplication.

of createObject function (name, Age) { // function set instantiated

var obj = new Object();

obj.name = name;

obj.age = age;

obj.run = function () {

return this.name + this.age + 'operation ...';

};

return obj;

}

 

box1 of createObject = var ( 'Lee', 100); // first instance

BOX2 of createObject = var ( 'Jack', 200 is); // second instance

alert(box1.run());

Alert (box2.run ()); // remain separate

 

Factory pattern to solve the repeated instances of the problem, but there is a problem, that is to identify the problem, because they simply can not figure out what in the end is an instance of an object.

alert(typeof box1); //Object

alert(box1 instanceof Object); //true

 

ECMAScript can be used in the constructor (the constructor) can be used to create specific objects. Type in Object object.

Box function (name, Age) { // Constructor mode

this.name = name;

this.age = age;

this.run = function () {

return this.name + this.age + 'operation ...';

};

}

 

Box new new box1 = var ( 'Lee', 100); // new new Box () to

was box2 = new Box ( 'Jack', 200);

alert(box1.run());

Alert (box1 instanceof Box); // very clear recognition of his subordinate Box

 

 

Using the constructor method, that is, to solve the repeated instances of the problem, but also solves the problem of object recognition, but the problem is that there are not new Object (), Why can instantiate Box (), this is where they come from it?

Using the constructor methods, and methods of their use the factory mode except for the following:

Object Create (new Object ()) constructor method is not displayed;

Properties and methods directly assigned to this object;

No renturn statement.

 

The method has some constructor specifications:

1. same function name and the instance name of construction and capital, (PS: not mandatory, but it was so written help distinguish between constructors and normal function);

2. Create an object in the constructor, you must use the new operator.

 

Now you can create objects through the constructor, then the object is where they come from, new Object () is executed in what place? The implementation process is as follows:

When using the constructor, and the new constructor (), is executed on the background of the new Object ();

The constructor's scope to the new object (that is, new Object () to create objects), while the body of the function of this represents new Object () out of the object.

Executes the code constructor;

Returns a new object (background direct return).

 

About this use of, this is actually representative of the current scope object. If this window represents the object in the global range, if the object in the constructor body, represents the current constructor declared.

each box = 2;

window.alert (this.box); // global, on behalf of window

 

The only difference between a constructor and general functions, that is, they call different ways. But, the constructor is also a function, we must use the new operator to call, otherwise, it is a normal function.

Box Box new new = var ( 'Lee', 100); // call configuration mode

alert(box.run());

 

Box ( 'Lee', 20); // Normal mode called invalid

 

var o = new Object();

Box.call (O, 'Jack', 200 is) // call objects posing

alert(o.run());

 

Discussion inside constructor method (or function) problem, first look at the properties or methods of the two instances are equal.

Box new new box1 = var ( 'Lee', 100); // consistent delivery

Box new new BOX2 = var ( 'Lee', 100); // supra

 

Alert (box1.name == box2.name); // to true, the value of the property is equal to

Alert (box1.run == box2.run); // false, the method is actually a reference address

Alert (box1.run () == box2.run ()); // to true, the value is equal to the method, because the same parameter passing

The constructor may be a method (or function) with new Function () method instead to give the same effect, more proof, they ultimately determines is a reference address, a unique property.

Box function (name, Age) { // new new Function () Uniqueness

this.name = name;

this.age = age;

this.run = new Function ( "return this.name + this.age + 'operation ...'");

}

 

Guess you like

Origin blog.csdn.net/wgf5845201314/article/details/92381075