1. Cause
I used prototype.js that day, so I opened it and took a look. I was confused after just reading a few lines. The reason was that I was not very familiar with the object-oriented nature of js, so I searched on Baidu and Google After a while, I finally gained a little bit, so I write this to commemorate it ^_^.
prototype.js code snippet
The code is as follows:
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
// Class is used as follows
var A = Class.create();
A. prototype={
initialize:function(v){
this . value=v;
}
showValue:function(){
alert(this.value);
}
}
var a = new A('helloWord!');
a. showValue();//Pop up the dialog box helloWord!
l What is initialize?
l What does the apply method do?
What about the arguments variable?
l Why is the initialize method executed after new A?
Find the answer:
2. Object-oriented Js
What is initialize?
It is just a variable, representing a method, and its purpose is the constructor of a class.
Its specific functions are supported by the object-oriented nature of js. So what does the object-oriented nature of js look like? What are the similarities and differences with java?
Look at the code:
The code is as follows:
The code is as follows:
var objectName1 = new ClassName("a");//Get an object
Among them, objectName1 is the object obtained after executing the ClassName constructor, and this in the ClassName function refers to the object constructed after new, so objectName1 will have one attribute and two methods. They can be called like this:
The code is as follows:
objectName1.setValue(''hello'');
alert(objectName1.getValue());//Dialog hello
alert(objectName1.value);//Dialogue box hello
Then
The code is as follows:
var objectName2 = ClassName("b");//Get an object
What does objectName2 get in this way? Obviously it is the return value of the method. Here ClassName is just used as an ordinary 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 "b" assigned to? An object is not generated here, but this method is simply executed, 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 b
So all functions in JS are the same, but their uses may be different (used to construct objects or execute a process).
It’s time to return to the topic. What does initialize do?
The code is as follows:
Copy code
The code is as follows:
3. call() and apply() Let’s start to study apply(). I found some information on the Internet and combined it with my own research to understand call() and apply( ) function. The functions are basically the same. The function of function().call(object,{},{}...) or function().apply (object,[...]) is that the object object calls the funciton() here. The difference is The call parameters are passed to funciton starting from the second one, and can be listed in sequence separated by ",". Apply only has two parameters. The second one is an array that stores all the parameters passed to the function.
this.initialize.apply(this , arguments); What does
mean?
The first this here refers to the object generated after calling the constructor with new, which is the previous a, so of course the second this should also refer to the same object. Then this sentence is that this (that is, a) calls the initialize method, and the parameter is the arguments object (array object of parameters), so when the constructor is executed, the object a will execute the initialize method to initialize, so that it is consistent with the word " initialize" means the right thing.
So how are the parameters for executing the initialize method passed in?
4. Arguments object
This code can explain everything: