I decided to be given the title of ajax, after all, many people would use this keyword to search. Although I think this is just a hype concept, I have to admit that it is much more convenient to call ajax. I won't explain the meaning of ajax in detail.
The reason for writing this tutorial is very simple: after a period of ajax learning, I have some experiences and I have become more aware of the power of ajax technology, so I decided to record it and organize my own ideas by the way.
In the past few years, JavaScript was still very narrow in the eyes of ordinary people. What it can do was either some simple form verification or many flashy web page special effects. With the emergence of flash, people are no longer as enthusiastic about js special effects as before. It seems that js can do less. But at this time, the concept of ajax emerged, and the ajax application represented by gmail was attracted by many people's attention. Ajax suddenly became a very popular technology. When JavaScript and XML and DOM models were combined, what they could do was often incredible, and some functions could even be comparable to desktop programs.
OK, I won’t say much nonsense. Now let’s start with a JavaScript development framework prototype_1.3.1 (hereinafter referred to as prototype). I originally wanted to introduce the advanced application of JavaScript first, but I was afraid that my level was not enough and I was not organized. So, in combination with prototype, I would mention the syntax use of js.
Here are the two codes in the first part of the framework:
var Prototype = {
Version: '1.3.1',
emptyFunction: function() {}
}
var Class = {
create: function() {
return function() {
(this, arguments);
}
}
}
First, let's look at the following two syntax differences:
var o={};
var f=function(){};
The latter one is easy to understand, it is equivalent to function f(){}; to define a function f. But the first one is not common: this is actually creating an object, in {}, you can specify the members of the object. For example, the above Prototype is an object with two members, the first is the version number, and the second is an empty method (function). Only js can do the function of creating objects directly without defining classes. The latter syntax actually has another function, which is to define a class f. If you use this in the function body, then the variables after this are members of the class.
Not only this can define class members, but there is also a syntax:
function c(){
member1:value,
member2:function(){}
}
This is equivalent to:
function c(){
this.member1=value;
this.member2=function(){};
}
It should be noted that when using the former method, the last member cannot be added to the end. I think this syntax should be related to the array.
In js, there is no difference between functions and classes. Both can be new. The function of new is to execute all the statements in the function body and then return an object. If there is this in the function, then the variable after this will be used as object members; if not, then the function of new is to return an empty object without any members. So when you use typeof to view a so-called class type, it will still return a function. There is basically no concept of type in js. All variable declarations use var, even functions. Functions are actually just variables.
Many people may not understand that functions are variables. But try the following:
function fTest(){
var a=1;
alert(a);
}
alert(fTest);
You will find that the function body of the fTest function is displayed, so we can think that the so-called function is just a piece of code string that the js engine can parse. The function name variable only stores this string. To put it more accurately, the function name is a pointer variable, which stores the location of this code string in memory. In this way, it is not difficult to understand that passing functions as parameters can be returned as values. This is a technique that will be widely used in the future. Because classes are also functions, after understanding functions, you also understand classes.
Although there is no difference between functions and classes in js, the concept of classes can facilitate us to design programming, so prototype creatively created a global object Class:
var Class = {
create: function() {
return function() {
(this, arguments);
}
}
}
Class is a global object, and its only way is to create, which returns a function. The mechanism of function as return value has been discussed earlier, so I will not talk about it here. The returned function includes a statement:
(this, arguments);
As mentioned earlier, when new a function, the code in the function will be executed and the object will be returned. So when a function is created using () and then new the returned function, this statement will be executed first. As you can see later, this is actually to call the class constructor.
That's it, Class has become a type creation model for the entire prototype, and can distinguish classes and functions in code well. () just returns an empty class, and it will default to this class that has an initialize method. Therefore, to use this class, at least one constructor is required, which requires the use of class inheritance. A class is just a function, so how do you inherit a function? It seems incredible. JavaScript can do this, and prototype makes the implementation more elegant. As for how it is done, let's listen to the next breakdown.
The reason for writing this tutorial is very simple: after a period of ajax learning, I have some experiences and I have become more aware of the power of ajax technology, so I decided to record it and organize my own ideas by the way.
In the past few years, JavaScript was still very narrow in the eyes of ordinary people. What it can do was either some simple form verification or many flashy web page special effects. With the emergence of flash, people are no longer as enthusiastic about js special effects as before. It seems that js can do less. But at this time, the concept of ajax emerged, and the ajax application represented by gmail was attracted by many people's attention. Ajax suddenly became a very popular technology. When JavaScript and XML and DOM models were combined, what they could do was often incredible, and some functions could even be comparable to desktop programs.
OK, I won’t say much nonsense. Now let’s start with a JavaScript development framework prototype_1.3.1 (hereinafter referred to as prototype). I originally wanted to introduce the advanced application of JavaScript first, but I was afraid that my level was not enough and I was not organized. So, in combination with prototype, I would mention the syntax use of js.
Here are the two codes in the first part of the framework:
var Prototype = {
Version: '1.3.1',
emptyFunction: function() {}
}
var Class = {
create: function() {
return function() {
(this, arguments);
}
}
}
First, let's look at the following two syntax differences:
var o={};
var f=function(){};
The latter one is easy to understand, it is equivalent to function f(){}; to define a function f. But the first one is not common: this is actually creating an object, in {}, you can specify the members of the object. For example, the above Prototype is an object with two members, the first is the version number, and the second is an empty method (function). Only js can do the function of creating objects directly without defining classes. The latter syntax actually has another function, which is to define a class f. If you use this in the function body, then the variables after this are members of the class.
Not only this can define class members, but there is also a syntax:
function c(){
member1:value,
member2:function(){}
}
This is equivalent to:
function c(){
this.member1=value;
this.member2=function(){};
}
It should be noted that when using the former method, the last member cannot be added to the end. I think this syntax should be related to the array.
In js, there is no difference between functions and classes. Both can be new. The function of new is to execute all the statements in the function body and then return an object. If there is this in the function, then the variable after this will be used as object members; if not, then the function of new is to return an empty object without any members. So when you use typeof to view a so-called class type, it will still return a function. There is basically no concept of type in js. All variable declarations use var, even functions. Functions are actually just variables.
Many people may not understand that functions are variables. But try the following:
function fTest(){
var a=1;
alert(a);
}
alert(fTest);
You will find that the function body of the fTest function is displayed, so we can think that the so-called function is just a piece of code string that the js engine can parse. The function name variable only stores this string. To put it more accurately, the function name is a pointer variable, which stores the location of this code string in memory. In this way, it is not difficult to understand that passing functions as parameters can be returned as values. This is a technique that will be widely used in the future. Because classes are also functions, after understanding functions, you also understand classes.
Although there is no difference between functions and classes in js, the concept of classes can facilitate us to design programming, so prototype creatively created a global object Class:
var Class = {
create: function() {
return function() {
(this, arguments);
}
}
}
Class is a global object, and its only way is to create, which returns a function. The mechanism of function as return value has been discussed earlier, so I will not talk about it here. The returned function includes a statement:
(this, arguments);
As mentioned earlier, when new a function, the code in the function will be executed and the object will be returned. So when a function is created using () and then new the returned function, this statement will be executed first. As you can see later, this is actually to call the class constructor.
That's it, Class has become a type creation model for the entire prototype, and can distinguish classes and functions in code well. () just returns an empty class, and it will default to this class that has an initialize method. Therefore, to use this class, at least one constructor is required, which requires the use of class inheritance. A class is just a function, so how do you inherit a function? It seems incredible. JavaScript can do this, and prototype makes the implementation more elegant. As for how it is done, let's listen to the next breakdown.