Javascript value types and reference types

Source: Internet
Author: User
Tags chrome developer chrome developer tools

This post consists of: http://xinpure.com/javascript-value-types-and-reference-types/

JavaScript variable type
  1. Value types: String, Number, Boolean, Null, Undefined

  2. Reference types: Arrays, objects, functions

Comparison of value types and reference types

Compare these two types with two instances (Debug environment: Chrome Developer Tools Console)

Value type instance
var a = ‘hello‘;var b = a;b;          /* hello */a = ‘world‘;b;          /* hello */

The value type of the variable assignment process, is actually allocated a new memory space, the same value but the two do not interfere

Reference type instance
/* 对象或数组 */var a = {"x": 0};var b = a;b.x;            /* 0 */a.x = 1;b.x;            /* 1 */a = {"x": 2};   /* 此时,a 指向了另一块内存空间,b 的值将不受干扰 */b.x;            /* 1 */a.x = 3;b.x;            /* 1 *//* 函数 */var a = new Function("alert(‘test‘)");var b = a;b();            /* 执行 alert(‘test‘) */

The variable assignment procedure for a reference type refers to two variables pointing to the same piece of memory space, where a change in the value of one variable causes a change in the value of the other variable

Javascript value types and reference types

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.