The examples in this article describe the methods of JavaScript primitive values and object references. Share it with everyone for your reference. The specific analysis is as follows:
In one sentence: primitive values are immutable, while object references are mutable.
Primitive values (undefined, null, Boolean values, numbers and strings) in js are essentially different from objects (including arrays and functions). The original value cannot be changed, and no method can change an original value; for strings, all methods in the string appear to return a modified string, but actually return a new string value. :
var str="hello world"; s.toUpperCase(); s; //仍然不变
Comparison of primitive values is a comparison of values: they only wait if their values are equal.
Objects are different from primitive values. First of all, they are mutable; secondly, the comparison of objects is not the comparison of values; the comparison of objects is the comparison of references: they only want to wait if and only if they refer to the same basic object. .
If you want to compare two separate objects or arrays, you must compare their properties or elements, as follows:
function equ_arrays(a,b){ if(a.length != b.lenght) return false; for(var i=0;i<a.length;i++) if(a[i] !== b[i]) return false; return true; }
I hope this article will be helpful to everyone’s JavaScript programming design.