Javascript variable type (figure)

Source: Internet
Author: User

Javascript variable types are really difficult to understand. Even if you are a very experienced js engineer, it is hard to say that you are clear about js types and classifications.

Recently, when I was talking about the javascript Getting Started Guide, someone suggested why arrays are not basic types. I demonstrated the result of typeof [] to explain that arrays are derived from object types, instead of six basic types.

In fact, it is very easy to understand the javascript variable type, that is, we need to find a division standard.

Javascript variable type classification

Javascript variable types are mentioned in many books. Each book has different criteria for Division. If the types are differentiated by the return values of typeof and instanceof, javascript variable types can be divided into two types: basic type and object type derived object type system.
Basic types include undefined, number, boolean, string, object, and function. They are previously distinguished by the return values of typeof.
The second set of object type systems is derived from the first set of systems, such as the previously mentioned Array, Null, Number, and Boolean. The object type can be determined by instanceof.
So what is the relationship between the Number in the object type and the number in the basic type? What are their differences?
The answer is that they are ing relationships, that is, the following example:

var a = new Number(123);console.log(a.valueOf()===123);//truevar b = new String(123);console.log(b.valueOf()===123);//falseconsole.log(b.valueOf()==='123');//true

Obj. valueOf () returns the original value of the object.

Value Type and reference type


Here we will talk about the value type and reference type. In javascript, undefined, string, number, and boolean are "Value Type", while object and function are "reference type ". All reference types can look at the subclass of Object (), so any function is also a subclass of Object.
How can we understand the value type and reference type ?? See the following example:

Value Type example
var a = 123;var b = a;a = 1;console.log(b);//123
Reference Type example
var c = [1,2,3];var d = c;d[0] = 4;console.log(c);//[4,2,3]
Description of value type and reference type

Seeing the example above, some people may be dizzy. Many people accidentally changed the value of the reference type, but they still don't know what went wrong with the program!
When Value Type a is assigned to B, space is allocated to B in the memory. Therefore, a and B are completely independent variables.
A value assignment produces a reference relationship between c and d, both of which point to the same array. Therefore, modifying a value changes the value of the other party.
Remember this in actual development. Do not assign values randomly. Otherwise, the above error will occur. For example, in the following example, you can assign values to the used site first, so that the variable e is a value type without reference issues.

var c = {site:'js8.in'};var d = c;var e = d.site;d.site = 'weibo.com';console.log(e);//js8.in
Arguments Value

In ECMAScript, function parameters are passed by value. When the parameter is a reference type value, passing by reference is an incorrect or incomplete statement.

It is easy to understand that a parameter is of the basic type. However, for parameters that reference type values, it is easy to misunderstand that they are passed by reference. Example:

function fn(arg){arg.site = 'js8.in';arg = new Object();arg.site = 'weibo.com';}var obj = new Object();fn(obj);console.log(obj.site)//js8.inconsole.log(window.arg);//undefined

In the example, if arguments is passed according to the reference type, obj. site should be weibo.com, but the result is js8.in.

The fact is that when the parameter is a reference type value, it is indeed passed by reference.
As for the example above, the reference is also passed. obj transfers the reference to arg. The memory space referenced by arg is the same as that of obj. Therefore, when site is set to js8.in, obj can be connected. After you create a new Object, it is equivalent to opening a new memory space, and arg leads to the new memory, but the memory referenced by obj is still the original one, so the site value is assigned, and the obj value remains unchanged. This is a bit like the C/C ++ pointer. As for window. arg is undefined because the js scope is lexical scope. Of course, the function cannot be referenced outside the function. It is related to value transfer or reference transfer wood (from Zhou mouxin ).

Finally, a diagram of the javascript type of Zhou aimin:

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.