Cause
I used Prototype.js that day, so I opened it to see that I saw a few lines and foggy. ^_^.
propotype.js code fragment
The code is as follows:
var class = {
Create: FUNCTION () {
Return function () {
this.initialize.apply (this, arguments);
}
}
}
// class use method is as follows
var a = class.create ();
A. Prototype = {
initialize: function (v) {
this. Value = v;
}
showvalue: function () {
alert (this.value);
}
}
var a = new a ('helloord!');
a. ShowValue (); // pop up the dialog box hellow!
l initialize?
What does the L Apply method do?
L arguments variable?
l why does the initialize method be executed after New A?
Find the answer:
Second, JS targeting object
What is initialize?
It is just a variable, representing a method, and the use is the constructor of the class.
The specific function is supported by JS, so what is the object of JS? What are the same and different from Java?
Look at the code:
The code is as follows:
Copy code code as follows:
var className = function (v) {{
this.value = v;
this.getValue = Function () {
Return this.value;
}
this.setValue = Function (v) {
this.value = v;
}
}
So what is the difference between the functions and classes in JS?
In fact, it is the same. ClassName is a function. When it appears behind New, it is used as a constructor to construct an object.
like
The code is as follows:
Var ObjectName1 = New ClassName ("A"); // Get an object
Among them, ObjectName1 is an object obtained after performing the ClassName constructor, and this in the ClassName function refers to the object constructed after the NEW, so ObjectName1 will have one attribute and two methods after the following. You can call them through this:
The code is as follows:
objectName1.setValue ('' hello '');
Alert (ObjectName1.GetValue ()); // Dialogue box hello
Alert (ObjectName1.Value); // Dialog box hello
So
The code is as follows:
var objectName2 = className ("B"); // Get an object
So what does ObjectName2 get? Obviously the return value of the method, here ClassName is only used as an ordinary function (although the first letter is capitalized). But there is no return value in the previous ClassName, so ObjectName2 will be undifinded so who does "B" give? There is no object here, but just performing this method simply, so this "B" is assigned to the object Window that calls this method. The evidence is as follows:
var objectName2 = className ("B"); // Get an object
alert (window.value); // dialog box B
So all the functions in JS are the same, but the use may be different (used for constructing objects or executing a process).
Let's go back to the theme what the initialize is doing?
The code is as follows:
Copy code code as follows:
var class = {
Create: FUNCTION () {
Return function () {
this.initialize.apply (this, arguments);
}
}
}
var a = class.create ();
This code is to construct a function copy to A, this function is
The code is as follows:
Copy code code as follows:
Function () {
this.initialize.apply (this, arguments);
}
And the latter method is used to make a constructor. When using this constructor to construct an object, it will let the initial () method of the initialialize variable of the constructor executed the Apply () method. The application of Apply () is later said to continue to talk about initialize. In this way, it will be contacted when initialized objects (how to contact the Apply).
So
The code is as follows:
Copy code code as follows:
A.prototype = {
initialize: function (v) {
this. Value = v;
}
showvalue: function () {
alert (this.value);
}
}
What does it mean?
Prototype means "prototype". A is a function (), then A. Prototype is a variable in Function, which is actually an object. What method does this object have, so what method does the object generated by the function?
var a = new a ('helloord!');
a. ShowValue (); // pop up the dialog box hellow!
Therefore, the A object will also have an initialize method. Not only that, every object with A constructor will have an initialize method. As mentioned earlier, the constructor will be called when the structure will be called. So when I ('HelloWord!'), INITIALIZE went back to call the Apply method. This is the method of calling an initialization.
3. Call () and Apply ()
Let ’s start studying Apply (), find a few information online, and combine your own research to understand the functions of call () and Apply ().功能基本一样,function().call(object,{},{}……)或者function().apply (object,[……])的功能就是对象object调用这里的funciton(),不同之处是The call parameter is passed to Funciton from the second. You can use "and" separate in order. Apply has only two parameters, and the second is a array, which stores all parameters passed to functions.
this.initialize.apply (this, arguments);
What does it mean?
The first this here refers to the object generated after calling the constructor with a new call, which is the previous A, then of course the second This should refer to the same object. Then this sentence is this (that is, A) to call the initialize method, and the parameter is an arguments object (the number of the array of the parameters). Therefore The meaning of initialize "is right.
So how to pass the parameters of the initialize method?
Fourth, Arguments object
This code can explain everything:
The code is as follows:
Copy code code 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 executing, Alert (Typeof Arguments); Object shows objects, indicating that arguments are objects. Then play 1, 2, and 3 in turn. Explain that arguments are the solid array of calling the function.
The code is as follows:
Copy code code as follows:
var class = {
Create: FUNCTION () {
Return function () {
this.initialize.apply (this, arguments);
}
}
}
Arguments are the solid array of constructor returned by Create, so
var a = new a ('helloord!');
At the time of 'HelloOWord!' Is the solid parameter group (although there is only one string), passed to the method Apply, and then passed to the initialization function initialize as a parameter when calling initialize.