Parse this.initialize.apply (this, arguments)

Source: Internet
Author: User

Excerpt from: http://www.cnblogs.com/uedt/archive/2010/06/24/1764561.html

First, causes
That day used to prototype.js so open look, just see a few lines on the head fog, the reason is the object of JS is not very familiar with, so Baidu +google A, finally calculate small have harvest, write this memorial ^_^.
Prototype.js Code Snippets

The code is as follows:
var Class = {
Create:function () {
return function () {
This.initialize.apply (this, arguments);
}
}
}
Class uses the following methods
var A = Class.create ();
A. prototype={
Initialize:function (v) {
this. Value=v;
}
Showvalue:function () {
alert (this.value);
}
}
var a = new A (' helloword! ');
A. Showvalue ();//Popup dialog box helloword!

What is L initialize?
l What does the Apply method do?
What about the L arguments variable?
L Why is the Initialize method executed after new a?
Find the Answer:

Second, JS object-oriented
What is initialize?
is simply a variable that represents a method, and the purpose is the constructor of the class.
Its specific functions rely on JS object-oriented support, then JS object-oriented What is it like? What is the same and different from Java?
Look at the code:

The code is as follows:
var ClassName = function (v) {
This.value=v;
This.getvalue=function () {
return this.value;
}
This.setvalue=function (v) {
This.value=v;
}
}

So what's the difference between a function and a class in JS?
In fact, ClassName is a function that constructs an object as a constructor when it appears behind new.
Such as

The code is as follows:
var objectName1 = new ClassName ("a");//Get an Object

Where objectName1 is the object that is obtained after executing the ClassName constructor, and this in the ClassName function refers to the object constructed after new, so objectName1 will have a property and two methods. You can call them by doing this:

The code is as follows:
Objectname1.setvalue (' ' Hello ');
Alert (Objectname1.getvalue ());//dialog box Hello
alert (objectname1.value);//dialog box Hello

So

Copy the code code as follows:
var objectName2 = ClassName ("b");//Get an Object

So what does ObjectName2 get? It is obviously the return value of the method, where classname is used only as a normal function (although the first letter is capitalized). But there is no return value in the previously written classname, so objectName2 will be undifinded so who is the "B" assigned to it? This does not produce an object, but simply executes the method, so this "B" assignment gives the object that invokes the method window, with the following evidence:
var objectName2 = ClassName ("b");//Get an Object
alert (window.value);//dialog box B
So all functions in JS are the same, but the use may be different (as a construction object or a process).
It's time to go back to the subject. What is initialize?

The code is as follows:
var Class = {
Create:function () {
return function () {
This.initialize.apply (this, arguments);
}
}
}
var A = Class.create ();

This code is to construct a function to copy to a, this function is

Copy the code code as follows:
function () {
This.initialize.apply (this, arguments);
}

And the following method is used to do the constructor function. When using this constructor to construct an object, the initialize variable of the constructed object is used to execute the Apply () method, and the use of apply () is said to continue to say initialize. This will contact the initialize when initializing the object (how to contact is to see apply).
So

The code is as follows:
a.prototype={
Initialize:function (v) {
this. Value=v;
}
Showvalue:function () {
alert (this.value);
}
}

What does that mean?
Prototype is the meaning of "archetype". A is a function (), then A. Prototype, which is a variable in function, is actually an object. What method does this object have, then the object that the function produces has the method, therefore
var a = new A (' helloword! ');
A. Showvalue ();//Popup dialog box helloword!
So a object will also have a initialize method, not only that, every object with a construct will have a initialize method, and in the previous said, the construction will call the constructor, the constructor will let initialize to invoke the Apply method, So in new A (' helloword! ') Initialize go back and call the Apply method. This is called an initialization method.

Iii. call () and apply ()
The following is the beginning of the study of apply (), the online search for a few materials, and combined with their own research, understand the call () and apply () function. Functions are basically the same as function (). Call (object,{},{} ...) or function (). Apply (object,[...]) The function of object called here is Funciton (), the difference is that the call parameter from the second start is passed to the Funciton, can be listed in turn "," separated. Instead, apply has only two parameters, and the second is an array that stores all the arguments passed to the function.
This.initialize.apply (this, arguments);
What is the meaning?
The first this here refers to the object that is generated after the constructor is called with new, that is, the previous a, and the second one should also refer to the same object. Then this is this (that is, a) call the Initialize method, the parameter is the arguments object (the array object of the parameter), so when the constructor executes, object a executes the Initialize method to initialize, and the word "initialize "The meaning is on the right.
So how do the arguments to execute the Initialize method pass in?

Iv. Arguments objects
This code can explain everything:

The code is as follows:
function Test () {
Alert (typeof arguments);
for (var i=0; i<arguments.length; i++) {
Alert (Arguments[i]);
}
}
Test ("1", "2", "3");
Test ("A", "B");

After execution alert (typeof arguments), an object is displayed, indicating that arguments is an object. It then hits 1, 2, 3 in turn. Description arguments is the real parameter group that invokes the function.

The code is as follows:
var Class = {
Create:function () {
return function () {
This.initialize.apply (this, arguments);
}
}
}

Arguments is the real parameter group of the constructor returned by create, then the
var a = new A (' helloword! ');
When the ' helloword! ' Is the real parameter group (though there is only one string), passed to the method apply, and then passed as a parameter to the initialization function initialize when calling initialize.

Parse this.initialize.apply (this, arguments)

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.