On primitive values and objects in JavaScript

Source: Internet
Author: User

JavaScript divides data types into two categories: primitive values (Undefined, null, Boolean, number, and string), objects (objects, functions, and arrays)

Argument: The original value can not be changed, the object can be changed, the object is a reference type;

' raw values can not be changed ' can not be changed, if the brains of the classmate will certainly doubt, you can change the string, do not believe you look at the following

var a = ' AA '; alert (A.touppercase ());//Eject AA

In fact, these are only superficial phenomena, the original value string ' A ' actually did not change, just copied a copy and then use the ' touppercase ' function on the new copy, the following code for proof

var a = ' AA '; a.touppercase (); alert (a) ; // eject ' AA '

Just staring, but the string ' a ' doesn't change.

The comparison of the original values is worth comparing, and their values are equal.

Objects are different from the original values, their values can be modified as follows

var obj = {' A ': 1, ' B ': 2= 3= 4; var arr = [1, 2, 3];arr[0] = 4;

Comparison of objects is worth comparing, even if two objects contain the same properties and values, they are also different


The following code is in evidence:

var a = {' X ': 1}, B = {' X ': 1= = = b); // false var c = [1], d = [1= = = d); // false

The values of the object are references, and the comparison of the objects is a reference comparison, and they are equal only if they refer to the same base object

' the value of the object is a reference ' the following code

varA = {' X ': 1};varb =a;a.x= 2; alert (b.x);//pop up 2varc =function(){    This. x = 1; }varD =NewC;varE =d;d.x= 3; alert (e.x);//pop up 3varAA = [1, 2, 3];varBB =aa;aa[0] = 11; alert (bb[0]);//Popup

' object comparisons are reference comparisons, and they are equal only when they refer to the same base Object ' as shown in the code below

var a = [1, 2, 3]; var b = a;a[1] = 5= = = b); // Popup True var  AA = [1, 2, 3]; var bb = [1, 2, 3= = BB); // Eject False

As we discussed above, the assignment of an object is simply a reference to an assignment, and no copy is made, and if you want to get a copy of a copy, you must explicitly copy each of the properties of the object or every element of the array, as follows

var a = [1, 2, 3, 4, 5]; var b = [];  for (var i =0, _len = a.length; i < _len; i++) {  = a[i];}

Similarly, if we want to compare two arrays or objects, we must compare each element or attribute of an array or an object.

My humble Caishuxueqian, there are shortcomings, welcome to top up!

On primitive values and objects 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.