This article is used to record learning notes;
JavaScript function This the use of the keyword –this keyword always points to the caller, who calls the function, this points to who call, apply the use of –call and apply the main application and the framework of the underlying, for the binding function of the execution environment/scope • Block concept – Unlike high-level programming languages, there is no block concept in JS, we generally wrap block-level scopes and closures with parentheses: Mastering closures must be well-understood concepts – execution Environment – scope chain – garbage collection mechanism with chestnut code attached
1 //The This keyword is used in JavaScript2 //The This keyword always points to the caller, who calls the function, and this points to who3 varx =5;//window.x = 5;4 function Test () {5Alert This. x); 6 };7Test.x =Ten;//give the function a static variable x8alert (test.x);//Ten9Window.test ();//5Ten One A //the main role of call apply is the binding scope - varcolor ='Red'; - varobj = {color:'Yellow'}; the - function Showcolor (x, Y, z) { -Alert (x+y+z); -Alert This. color); + }; - ////Call, apply binds a function to execute within the scope you specify +Showcolor.call (window,Ten, -, -);//Red AShowcolor.call (obj,Ten, -, -);//Yellow atShowcolor.apply (obj, [2,3,4]);//Yellow 9 - - //concept of block: - function Test () { - (function () { - for(vari =1; I <=5; i++){ in alert (i); - } to })(); +alert (i);//undefined - }; the test (); * $ //closures: Execution environment, scope chain, JS garbage collectionPanax Notoginseng //The function returns directly to the return, without renturn returning the result undefined - //in the JavaScript language: The use of global variables (1: Unsafe, 2: Variable search finding is less efficient) the function Test () { + returnfunction () { AAlert'I am the closure of the package body code ...'); the }; + }; - varf =test (); $Alerttypeoff);//function $f ();//I am the closure of the package body code ... - - function Test () { the vartemp =Ten ; - returnfunction () {Wuyi alert (temp); the }; - }; Wu test () (); - About $ //Avoid using global variables click buttons to count clicks - varINP =(function () { - vari =0; - return { A getcount:function () { +Alert (+ +i); the } - }; $})();
Recommended to you a beautiful article network www.fishcmonkey.com, learning to improve literary accomplishment