JS talking about primitive value and reference value operation

Source: Internet
Author: User

The operation of the value is divided into three main categories: Copy, pass, compare one: Copy
original value let a= 10; let b=A; Note:2018-7-30 17:33:491 The values of the original type are stored in the stack memory , so their assignment operation is actually the equivalent of opening up new space in the stack memory, and then assigning a copy of the value to the new memory, so they do not interfere with the reference value let obj={name:' Panrui '};let obj1=obj;1 The value of the reference type is stored in the heap memory when the variable in the stack memory is just the address of a heap of memory, so the assignment operation is also to open up a new stack of memory, and then assign the address to the new memory, because the two variables corresponding to the address point to the same place, So they're going to affect each other  .2Tip: What if you assign a new value to a variable?2.1obj1= 6console.log (obj)//{name: ' Panrui '} does not affect2.2obj= 6Console.log (obj1)//{name: ' Panrui '} does not affect    a duplicate assignment is actually a reference to the original value that overrides the variable, to a copy of another value, or to a reference to it. So it doesn't have an effect on the original value .

Second: Delivery
original value let a= 10;functionf (x) {x= x +X}f (a) Console.log (a)//TenNote: The description of the delivery and assignment is actually the same, is a copy of the passed value, do not affect the reference value let obj={name:' Panrui '};functionA (a) {A.name= ' Hello '}function(obj) console.log (obj)//{name: ' Panrui '} because the passed address points to the same reference, so it affects each other Note: The description whether the base type or the reference type is passed by value, the reference type passes the address, and a copy of the value passed by the base typefunctionA (a) {A.name= ' Hello '; A=NewObject (); A.name= ' hah '; Console.log (a)//{name: ' hah '} is equivalent to a re-assignment, when a is stored in the stack memory is a copy of another value or a new address}a (obj) console.log (obj)//{name: ' Hello '}
Three: Comparison values
= ' Hello '= ' Hello '//true Note: When comparing the original values, a byte-by-bit comparison is made to determine whether they are equal. The comparison is the value itself  New Number (1new number (1//false=//  true Note: When comparing two reference values, compare two reference addresses instead of comparing their original value bytes for equality

js talking about primitive value and reference value operation

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.