Site Front end _javascript.0012.javascript deep objects

Source: Internet
Author: User

Basic type:

Description: The basic type variable is mainly stored in the stack memory variable name + variable value, variable assignment when the parser determines that this value is the base type, then allocates a fixed size of space, the base type includes undefined, Null, Boolean, number, string literal object, Called a base type Object

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/87/18/wKiom1fTxZ6wYGANAAAr1GV8uK4852.png-wh_500x0-wm_3 -wmp_4-s_158329954.png "title=" Basic type "alt=" Wkiom1ftxz6wyganaaar1gv8uk4852.png-wh_50 "/>

var name = ' Li full '//Copy a "Li full" in the stack memory to newnamevar newName = Namenewname = ' Liu Jianjin '//due to the assignment of the copy and the original data is not a half-dime relationship, So any variable of the basic type will not change the value of the other variable Console.log (name, newName)


Reference type:

Description: The reference type variable because the memory size is not fixed, but the memory address is fixed, so the variable name + data in the heap memory address is stored in the stack memory, the value of the variable is stored in the heap memory, when the query reference type variable, the memory address is read from the stack, and then the address to find the value in heap memory, Reference types include Boolean, number, String, Function, Array, object, Date, RegExp, Error, Map, set object, and []/{} generated arrays and objects, etc., called reference type objects

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/87/18/wKiom1fTxc_TSAuuAAA8VqJP51w232.png-wh_500x0-wm_3 -wmp_4-s_2852783582.png "title=" reference type "alt=" Wkiom1ftxc_tsauuaaa8vqjp51w232.png-wh_50 "/>

var userInfo = {name: ' li full '}//copies a userInfo memory address to Newuserinfo in the stack memory, at which point UserInfo and Newuserinfo have the same memory address var newuserinfo = Useri Nfonewuserinfo.name = ' Liu Jianjin '//Because the memory address of UserInfo and Newuserinfo is the same, so the address in the heap memory is the same, so modifying a variable of any reference type causes the data in the heap memory to change. Other variables that point to the heap memory will change Console.log (Userinfo.name, Newuserinfo.name)


Pass parameters:

Note: The parameters of all functions in JS are passed by value, passing reference parameters and reference arguments is not a concept, the function supports passing the basic and reference parameters, but the parameters are not passed by reference, if the reference is passed, the variables in the function will be global variables, can be accessed externally

Function-passed by value, non-modifiable function numadd (num) {num++ return Num}var num = 10//is passed by Num by value, num++ is the value of copy num and then +1, the original num is unaffected Consol E.log (num, Numadd (num), num)//function-passed by value, can modify function Useredit (userInfo) {userinfo.name = ' Liu Jianjin ' return Userinfo}var UserInfo = {name: ' Li-Full '}//because UserInfo objects are passed by value, Userinfo.name= ' Li-Full ' is the heap address of the copy userInfo, So modifying the heap data corresponding to the same heap address another reference variable corresponding to this heap address will also be changed Console.log (Userinfo.name, Useredit (userInfo), userinfo.name)


Detection type:

Description: A typeof expression is available for detecting the base type object, which prints the type string representation of the corresponding type object, but the reference type object is basically returned with object if using typeof detection, and we prefer to know the original type of the reference type object. You can use the instanceof expression to determine

var strorgobj = ' Python ' var strrefobj = new string (' Python ')//Strorgobj for original string type object exists in stack memory, strrefobj as reference type Object (Reference child primitive String type Object ), stored in heap memory Console.log (typeof Strorgobj, typeof Strrefobj, Strrefobj instanceof String)

Note: instanceof only supports reference type Object type detection, and returns false for checking the value of the base type


function Environment:

Description: The execution environment defines other data that a variable or function has access to, and each execution environment is associated with an object, and the global execution environment is considered a Window object, so all global variables and functions are created as properties and methods of the Window object. Local variables also have a window-like variable object, and the variables and functions defined in the environment are stored in the object.

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/87/16/wKioL1fTxhqQTsUqAAA8VqJP51w254.png-wh_500x0-wm_3 -wmp_4-s_575015931.png "title=" function Environment "alt=" Wkiol1ftxhqqtsuqaaa8vqjp51w254.png-wh_50 "/>

Global scope-The global scope is associated by default with the Window object var userName = ' Li full ' console.log (window.username)//local scope-local scope by default is associated with the nearest parent class object function Sort (arr) {//The CMP method can only be called within the sort function, and function CMP cannot be called externally (A, b) {a = parseint (a) b = parseint (b) RET Urn a>b?1: (a==b?0:-1)} return Arr.sort (CMP)}console.log (sort ([1, 11, 2, 22, 3, 33]))

Note: Each function will create its own execution environment at the time of invocation, when executing to this function, the environment of the function will be pushed to the environment stack to execute, then after executing and then popping in the stack, giving control to the execution environment at the upper level, and automatically creating the scope chain when the function is called. When querying variables in a scope, it is up to the scope front-end to look up and out, so it is strongly not recommended not to initialize the variable with var because this method can cause various unexpected

Note: When all the code in the execution environment is executed, the environment is destroyed, and all the variables and functions stored therein are destroyed, such as in a global variable environment where the program execution is completed or the Web page is closed before it is destroyed


Memory Issues:

Description: JavaScript has an automatic garbage collection mechanism, but the garbage collector mechanism is cycle-running, generally ensuring that the minimum memory is used for better performance, then the best way to optimize memory is to mark the deletion (set to null) once the data is no longer useful

var userInfo = {name: ' li full '}userinfo = Nullconsole.log (userInfo)



This article is from the "ζ Automated operation and maintenance development Road ζ" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1851415

Site Front end _javascript.0012.javascript deep objects

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.