Computational problems in JavaScript

Source: Internet
Author: User

A total of six types of data in JS:

Five types of value: Boolea number String Null undefined

Reference type: Object----Three major reference types: Object Array Function

Topic 1:var A = 100;

var B = A;

A = 200;

Console.log (b);

Topic 2:var A = {age:20};

var B = A;

B.age = 21;

Console.log (A.age);

The answer to Topic 1 is 100, the answer to question 2 is 21,

Topic 1 is a simple value type that, when you assign a base type from one variable to another, creates a new value on the variable and then copies the value to the location assigned to the new variable.

At this point, the value saved in a is 100, and when a is used to initialize B, the value saved in B is also 100, but 100 in B is completely separate from a, which is just a copy of the value in a, after which,
These two variables can participate in any operation and are unaffected by each other. That is, after the assignment operation, the base type has two variables that are not affected by one another.

Topic 2 is a reference type, and when a value of a reference type is assigned to another variable from one variable, the value of the object stored in the variable is also copied into the space allocated for the new variable.

At this point in the variable is the address of the object in the heap memory, so, unlike the simple assignment, the copy of this value is actually a pointer, and this pointer points to an object stored in the heap memory. Then after the assignment operation,
Two variables all hold the same object address, the two variables point to the same object. Therefore, changing any one of these variables will affect each other.

Therefore, the assignment of a reference type is actually an assignment of the object's value in the stack address pointer, so that two variables point to the same object, and any operation will affect each other.

Computational problems in JavaScript

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.