JavaScript functions define and invoke variable scopes

Source: Internet
Author: User
Tags variable scope

This is the author's personal summary of Liaoche's JavaScript tutorial. The function definition in JavaScript can be such a format functions name (parameter) {function body }It can also be in this format var 函数名 = function (参数) {function body };Keyword one: Arguments gets all parameters that work only inside the function and always points to all arguments passed in by the caller of the current function. argumentsSimilar ArrayBut it's not a Array。The first parameter is Arguments[0] .... [n] keyword two: rest gets to write this when a parameter definition function other than the defined parameter is obtained function foo(a, b, ...rest)If the parameter is normal, rest is an empty array variable scope if a variable is declared inside the function body, the variable is scoped to the entire function body, and the variable is not referenced outside the body of the function if                     Two different functions each declare the same variable, then the variable only works in its own function. Because JavaScript functions can be nested, the intrinsic function can access the variables defined by the external function, and vice versa. JavaScript functions look for variables, starting with their own function definitions, and from inside to outside. If the intrinsic function defines a variable with the same name as an external function, the variable of the intrinsic function will "mask" the variable of the outer function variable PromotionJavaScript's function definition has a feature, it will first scan the entire function body of the statement, all the declared variable "ascend" to the top of the function JavaScript engine automatically promoted the declaration of the variable, but does not increase the value of the variable assignment. A global scope does not have a global scope for variables defined within any function. In fact, JavaScript has a global object by default window, the variables of the global scope are actually bound to windowA property of JavaScript actually has only one global scope. Any variable (function is also considered a variable), if not found in the scope of the current function, will continue to look up, and finally if not found in the global scope, it is reported referenceerror error block-level scope in order to resolve the block-level Scope, ES6 introduces a new keyword letUse letAlternative varYou can declare a block-scoped variable (for loop) constant ES6 Standard introduces new keywords constTo define constants, constAnd letMethods that have block-level scope objects: 1. Inside an object method, thisis a special variable that always points to the current object. To ensure that this point is correct, you must use Obj. Method 2. Place the object's method outside the definition and internally through a property to the function name. If the function is called directly externally, this points to the Global object window. In static mode, this pointer to the function undefined, so in static mode, you get an error 3.this pointer only points to the object in the object's function, if the function within the object's function is still pointing to undefined. If you want to use this in a function's function, you can first declare a that variable to capture this, and where you use this in the function's function, change to that 4. How to control the pointer to this in the function, you can use the Apply method of the function's own. It receives two parameters, the first parameter is the one that needs to be bound thisVariable, the second parameter is ArrayA parameter that represents the function itself. Normal function The This pointer value is a NULL execution format: the function name. Apply (The object (or null) to which the function itself requires a list of parameters to be passed in an array]) functions also bring a call method , the difference between call and apply is that
          • apply () package the parameters into a Array and then pass in;
          • call () passes parameters sequentially.
                    5. Implement an adorner with apply.                           Step 1. The function that needs to be replaced is the one that needs to be refactored or added function Fun ()                                 &NBSP ;       var oldfun = fun;                                      fun = function () {                                              Add-on function code &NBS P                          ,         &NB Sp         return oldfun.apply (null,arguments);                &N Bsp                        }                    & nbsp;                         

JavaScript functions define and invoke variable scopes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.