Home > Web Front-end > JS Tutorial > JavaScript original value and object reference instance analysis_javascript skills

JavaScript original value and object reference instance analysis_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:02:25
Original
1173 people have browsed it

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;     //仍然不变
Copy after login

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;
}
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template