JavaScript Red Book notes functions, variables, parameters, scopes and memory issues

Source: Internet
Author: User

ECMAScript don't mind passing in the number of parameters, nor mind the type of arguments passed in. Understanding Parameters:Named parameters are provided only for convenience and are not required. The ECMAScript variable contains values for two different data types. respectively is
    • Simple data constitutes a basic data type value that cannot be added to a base value attribute:
var name = "Kevin";          Name.age = 27;     Console.log (Name.age); Undefined
    • Multiple values constitute an object that references a type value. The value of a reference type , you can add a dynamic property, and you can change the value of the dynamic property or you can delete the value of the dynamic property
var name = new Object ();          Name.age = 27;     Console.log (Name.age); 27 The ECMAScript action object is actually a reference to an object in action, not an actual object. Copy variable values
    • The value of the base type
var name = "Kevin";        var name2 = name; The value of name Kevin;name2 is also Kevin; they participate in any action in their respective scopes without affecting each other, because name2 are independent and non-intrusive.
    • Reference type value
var name = new Object ();          var name2 = name;          Name.age = 27;   Console.log (Name.age); 27 Unlike the value of the underlying type above, the name2 is actually a pointer to name, and this pointer points to an object of name, so this name2 also refers to an object and is the same object as name. Passing Parameters The parameters of all functions in ECMAScript are passed by value.
    • Parameter passing of a primitive type, passed by value
When you pass a value of a primitive type to a parameter, the passed value is copied to a local variable, which is an element in the arguments object.               function Add (num) {num = 30;      Console.log (num);          This parameter is actually the local variable of the function, and its value is still $10} var = num2          var num3 = Add (num2);          Console.log (NUM2);          Ten Console.log (NUM3); 40, when this function is called, the variable num2 is passed to the function as a parameter. The value of this addr () function is the value of the local variable of 30, plus the value passed in num2 = 10, the last num3 value is 40
    • Parameter passing of a reference type, passed by object
          in other words, even if the variable is passed by value, the passed value accesses the same object by reference. That is, even if the value of the parameter is modified inside the function, the original reference remains unchanged.           function setName (obj) {              Obj.name = ' Kevi n ';          //obj The default Name property value is kevin              obj = new Object ( );          //redefining a new object for obj, this object is a local object               Obj.name = ' G Reg ';          //define a different value for this new local object Name property           }            Console.log (obj.name);          //kevin,   Why the value here is Kevin, because, in fact, when you rewrite obj inside a function, the reference to that variable is actually a local object, This local object is destroyed immediately after the function is executed            var person = new Object ();          SetName (person);          Console.log (person.name);      //KEVIN&NBSP you can think of the parameters of the ECMAScript function as local variables.The execution Environment execution Environment defines other data that a variable or function has access to, determines their own behavior, and each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in the object. The code we have written cannot access this object.
    • The global execution environment is not destroyed until the application exits, such as when the Web page or browser is closed.
Each function has its own execution environment.     var name = ' Kevin ';          function ChangeName () {if (name = = = ' Kevin ') {name = ' page ';          }else{name = ' Kevin ';     }} changename (); Console.log ("name is now" + name);

JavaScript Red Book notes functions, variables, parameters, scopes and memory issues

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.