Home  >  Article  >  Web Front-end  >  Are there pointers in javascript?

Are there pointers in javascript?

WBOY
WBOYOriginal
2022-07-01 15:36:513268browse

There are no pointers in JavaScript, only references; the pointer refers to the memory address. Being able to operate the pointer means that the program can directly operate the memory. However, when the JavaScript script is run on the web page, neither the browser nor the operating system allows JavaScript to be used. Manipulate memory, so there is no concept of pointers in JavaScript scripts, and memory can only be allocated by the browser.

Are there pointers in javascript?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

Are there pointers in JavaScript?

JS does not have pointers, only references.

A simple basis for judgment is: pointers can be manipulated, but JS cannot.

The pointer is actually the memory address. Being able to operate the pointer means that the program can directly operate the memory. This is very dangerous for js scripts running on the web page. Neither the browser nor the operating system will allow such a thing. occurs, so js scripts cannot have the concept of pointers and can only reference the memory allocated by the browser. As for what the physical address of this memory is, it is opaque at least to js programmers.

The example is as follows:

let a = 888
let b = a
a = 999
console.log(a, b) // 999 888 对于『基本类型』的变量,a,b 相互是不影响的

However, for composite type variables, the variable actually stores a "pointer". When d=c, they Points to the same memory address, so when c changes, d also changes: let c = ['a', 'b', 'c']

let d = c
d === c // true 都指向同一个『指针』呢
c[0] = 'x'
console.log(d) // ["x", "b", "c"]
而这样let c = ['a', 'b', 'c']
let d = ['a', 'b', 'c'] // or 简写成 d = [...c]
d === c // false 变量 d 重新赋值了呢,所以和 c 就不是一伙的啦!
c[0] = 'x'
console.log(d) // ["a", "b", "c"]

Are there pointers in javascript?

Expand knowledge:

Pointers are an important concept and feature in C language. They are also a difficult part of mastering C language. Pointers are memory addresses. Pointer variables are variables used to store memory addresses. Under the same CPU architecture, different types of pointer variables occupy the same storage unit length, while variables that store data vary depending on the type of data. The length of storage space occupied is also different. With pointers, you can not only operate on the data itself, but also on the variable address where the data is stored.

The pointer describes the location of data in memory, marking an entity occupying storage space and the relative distance value from the starting position of this space. In C/C language, pointers are generally considered to be pointer variables. The content of a pointer variable stores the first address of the object it points to. The object pointed to can be a variable (pointer variables are also variables), arrays, functions, etc. occupy storage space. entity.

【Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of Are there pointers in javascript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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