In the Codefordream to enter the intermediate study, feel immediately from the basic JS learning to jump forward, the above things seem to be the first time to see the same. At this time only to find, said has been contacted with JS, but only then found that the understanding of JS stopped in know two: JS in the definition of var variables, with function definition functions! At this time a bit of shame, say really sorry teachers teach! Before the contact JS is used to write Web pages, at that time in the Dreamwever to write a lot of features do not do their own functions to implement, but directly drag components and other methods to create, resulting in the like really in addition to see the JS code, I really did not write!
The intermediate parts are mainly: Code running rules, classes and objects, this reference, prototype inheritance, and callback functions need to learn knowledge.
A About this reference:
1. When the function calls the mode, this points to the window.
2. When the method invokes the pattern, this points to the object where the method is located
3. In the constructor mode, this points to the newly generated instance
When 4.apply/call invokes a pattern, this points to the first parameter in the Apply/call method
For a function, we can point the this reference inside the function to a specific object by invoking the Apply method of the function (note that the function is also an object). Eg:
1 function Tool (name, usage) {2 This. Name =name;3 This. Usage =usage;4 This. Get_info = function () {Console.log (" This is"+ This. Name +", the usage of it is"+ This. Usage +".");};5 }6 varCycling =NewTool ("Cycling","Transport");7 function Display (date) {8Console.log ( This. name+","+ This. usage+","+date);9 }TenDisplay.apply ({name:"Cycling", Usage:"Transport"},[1970]); One //cycling,transport,1970
b About classes and objects: JS has a function similar to the ' class ' function, called a constructor, which is a function that can be used to generate objects. The object in JS is a set of unordered sets of properties, each of which can be a simple data type value or an object or a function. Eg:
1 varRobot = {//The constructor object () generates an Robot2Birthday'January 1, 1970',3Name'Deepsky',//define name and birthday two properties4Say_hello:function () {Console.log ("My name is ""+ This. name+", is a born in"+ This. birthday+"of robots.");}//Add the Say_hello method to the object so that the method can output a word in the console that contains the name and birthday property values.5};
C About callback functions
In JavaScript, functions are actually objects: they can be "stored" in variables, can be passed as function parameters, can be created in functions, and can be returned from functions.
1 //Global Variables2 varinfo = [];3 4 //Normal show function to print the contents of the data to the console5 function Show (data) {6 Info.push (data);7 //Direct Output If it is a string that can be output directly8 if(typeofdata = = ="string")9 {Ten console.log (data); One } A Else if(typeofdata = = ="Object"){ - //Traverse Data - for(varIteminchdata) { theConsole.log (item +": "+Data[item]); - } - } - } + - //define a function that receives two parameters, the next one in the argument is the callback function, the callback function is called in the function body, and the first parameter is passed to the callback function. + function Get_inputs (data,callback) { A callback (data); at - } - //when we call the Get_inputs function, we pass the show function as a parameter to it - //so show will be callback (or execute) within the Get_inputs function -Get_inputs ({name:"Bower", Speciality:"Robot"}, show); - //The final output is as follows: in //Name:bower - //Speciality:robot
D About prototypes and objects:
I feel that there are some problems in understanding, and then understand a little, will make up!
Here are the understandings of other friends: http://www.cnblogs.com/wfsovereign/p/4276694.html
My JS Intermediate Learning Chapter