1. Cause
That day, I opened it and looked at it. I was confused after just a few lines. The reason was that I was not very familiar with object-oriented js, so I made a handful of Baidu + Google, and finally I got some gains. Write this to commemorate ^_^.
Code snippet
var Class = {
create: function() {
return function() {
(this , arguments);
}
}
}
// Class is used as follows
var A = ();
A. prototype={
initialize:function(v){
this .value=v;
}
showValue:function(){
alert();
}
}
var a = new A(‘helloWord!');
a. showValue();//The dialog box helloWord pops up!
What is l initialize?
What is the apply method?
What about l arguments variable?
l Why does the initialize method be executed after new A?
Looking for answers:
2. Object-oriented Js
What is initialize?
It's just a variable, representing a method, and its purpose is the constructor of the class.
Its specific functions are supported by object-oriented by js. So what does object-oriented by js look like? What are the same and differences with Java?
Look at the code:
var ClassName = function(v){
=v;
=function(){
return ;
}
=function(v){
=v;
}
}
So what are the differences between 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 the object.
like
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 by this:
(''hello'');
alert(());//Dialogue hello
alert() ;//Dialogue hello
So
var objectName2 = ClassName("b");//Get an object
What does objectName2 get in this way? Obviously it is the return value of the method, and here ClassName is only used as an ordinary function (although the initial letter is capitalized). However, there is no return value in the ClassName I wrote before, so objectName2 will be undiminated, so who will "b" be assigned to? There is no object generated here, but just executes 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(); //Dialogue b
So all functions in JS are the same, but their uses may be different (used as a constructor or performing a process).
Now it’s time to return to the topic. What does initialize do?
var Class = {
create: function() {
return function() {
(this , arguments);
}
}
}
var A = ();
This code is to construct a function to copy it to A. This function is
function() {
(this , arguments);
}
And the latter method is used as a constructor. When using this constructor to construct an object, the initialize variable of the constructed object will be allowed to execute the apply() method. The purpose of apply() will be discussed later, and continue to talk about initialize. In this way, when initializing the object, initialize will be contacted (how to contact depends on the apply).
So
={
initialize:function(v){
this .value=v;
}
showValue:function(){
alert();
}
}
What does it mean?
Prototype means "prototype". A is a function(), so A. prototype is a variable in function, which is actually an object. Whatever method this object has, then what method the object generated by function has, so
var a = new A(‘helloWord!');
a. showValue();//The dialog box helloWord pops up!
Therefore, object a will also have an initialize method. Not only that, every object constructed with A will have an initialize method. As mentioned earlier, the constructor will be called during construction, and the constructor will ask initialize to call the apply method. So when new A ('helloWord!'), initialize will go back to call the apply method. This is to call an initialization method.
3. call() and apply()
Let’s start studying apply() below, and found a few information online, and combined with my own research, I learned about the functions of call() and apply(). The functions are basically the same. The function of function().call(object,{},{}...) or function().apply (object,[...]) is that the object object calls funciton() here. The difference is that the call parameters are passed to funciton from the second start, and they can be listed in sequence and separated by ",". apply only has two parameters, the second is an array, which stores all the parameters passed to the function.
(this , arguments);
What does it mean?
The first this here refers to the object generated after calling the constructor with new, that is, the previous a, so the second this should of course refer to the same object. Then this sentence means that this (that is, a) calls the initialize method, and the parameter is the arguments object (array object of the parameter), so when the constructor is executed, object a will execute the initialize method to initialize, which matches the meaning of the word "initialize".
So how do you pass the parameters of the initialize method in?
4. Arguments object
This code tells it all:
function test(){
alert(typeof arguments);
for(var i=0; i<; i++){
alert(arguments[i]);
}
}
test("1","2","3");
test("a","b");
After execution, alert(typeof arguments); will display object, indicating that arguments are objects. Then 1, 2, and 3 will be typed in turn. Explain arguments are the actual parameter group that calls the function.
var Class = {
create: function() {
return function() {
(this , arguments);
}
}
}
arguments is the actual parameter group of the constructor returned by create, so in
var a = new A(‘helloWord!');
When 'helloWord!' is the real parameter group (although there is only one string), passed to the method apply, and then passed as a parameter to the initialization function initialize when initialize is called.
That day, I opened it and looked at it. I was confused after just a few lines. The reason was that I was not very familiar with object-oriented js, so I made a handful of Baidu + Google, and finally I got some gains. Write this to commemorate ^_^.
Code snippet
Copy the codeThe code is as follows:
var Class = {
create: function() {
return function() {
(this , arguments);
}
}
}
// Class is used as follows
var A = ();
A. prototype={
initialize:function(v){
this .value=v;
}
showValue:function(){
alert();
}
}
var a = new A(‘helloWord!');
a. showValue();//The dialog box helloWord pops up!
What is l initialize?
What is the apply method?
What about l arguments variable?
l Why does the initialize method be executed after new A?
Looking for answers:
2. Object-oriented Js
What is initialize?
It's just a variable, representing a method, and its purpose is the constructor of the class.
Its specific functions are supported by object-oriented by js. So what does object-oriented by js look like? What are the same and differences with Java?
Look at the code:
Copy the codeThe code is as follows:
var ClassName = function(v){
=v;
=function(){
return ;
}
=function(v){
=v;
}
}
So what are the differences between 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 the object.
like
Copy the codeThe 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 by this:
Copy the codeThe code is as follows:
(''hello'');
alert(());//Dialogue hello
alert() ;//Dialogue hello
So
Copy the codeThe 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, and here ClassName is only used as an ordinary function (although the initial letter is capitalized). However, there is no return value in the ClassName I wrote before, so objectName2 will be undiminated, so who will "b" be assigned to? There is no object generated here, but just executes 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(); //Dialogue b
So all functions in JS are the same, but their uses may be different (used as a constructor or performing a process).
Now it’s time to return to the topic. What does initialize do?
Copy the codeThe code is as follows:
var Class = {
create: function() {
return function() {
(this , arguments);
}
}
}
var A = ();
This code is to construct a function to copy it to A. This function is
Copy the codeThe code is as follows:
function() {
(this , arguments);
}
And the latter method is used as a constructor. When using this constructor to construct an object, the initialize variable of the constructed object will be allowed to execute the apply() method. The purpose of apply() will be discussed later, and continue to talk about initialize. In this way, when initializing the object, initialize will be contacted (how to contact depends on the apply).
So
Copy the codeThe code is as follows:
={
initialize:function(v){
this .value=v;
}
showValue:function(){
alert();
}
}
What does it mean?
Prototype means "prototype". A is a function(), so A. prototype is a variable in function, which is actually an object. Whatever method this object has, then what method the object generated by function has, so
var a = new A(‘helloWord!');
a. showValue();//The dialog box helloWord pops up!
Therefore, object a will also have an initialize method. Not only that, every object constructed with A will have an initialize method. As mentioned earlier, the constructor will be called during construction, and the constructor will ask initialize to call the apply method. So when new A ('helloWord!'), initialize will go back to call the apply method. This is to call an initialization method.
3. call() and apply()
Let’s start studying apply() below, and found a few information online, and combined with my own research, I learned about the functions of call() and apply(). The functions are basically the same. The function of function().call(object,{},{}...) or function().apply (object,[...]) is that the object object calls funciton() here. The difference is that the call parameters are passed to funciton from the second start, and they can be listed in sequence and separated by ",". apply only has two parameters, the second is an array, which stores all the parameters passed to the function.
(this , arguments);
What does it mean?
The first this here refers to the object generated after calling the constructor with new, that is, the previous a, so the second this should of course refer to the same object. Then this sentence means that this (that is, a) calls the initialize method, and the parameter is the arguments object (array object of the parameter), so when the constructor is executed, object a will execute the initialize method to initialize, which matches the meaning of the word "initialize".
So how do you pass the parameters of the initialize method in?
4. Arguments object
This code tells it all:
Copy the codeThe code is as follows:
function test(){
alert(typeof arguments);
for(var i=0; i<; i++){
alert(arguments[i]);
}
}
test("1","2","3");
test("a","b");
After execution, alert(typeof arguments); will display object, indicating that arguments are objects. Then 1, 2, and 3 will be typed in turn. Explain arguments are the actual parameter group that calls the function.
Copy the codeThe code is as follows:
var Class = {
create: function() {
return function() {
(this , arguments);
}
}
}
arguments is the actual parameter group of the constructor returned by create, so in
var a = new A(‘helloWord!');
When 'helloWord!' is the real parameter group (although there is only one string), passed to the method apply, and then passed as a parameter to the initialization function initialize when initialize is called.