JS里有指针么?

简介: 提到 js 指针,其实我是懵圈儿的,准确来说,应该是没有的,引用倒是满天飞。不过下面的这个例子值得看看。Are there pointers in javascript?No, JS doesn’t have pointers.Objects are passed around by passing a copy of a reference. The p

提到 js 指针,其实我是懵圈儿的,准确来说,应该是没有的,引用倒是满天飞。不过下面的这个例子值得看看。

Are there pointers in javascript?

No, JS doesn’t have pointers.

Objects are passed around by passing a copy of a reference. The programmer cannot access any C-like “value” representing the address of the object.
Within a function one may change the contents of an passed object via that reference, but you cannot modify the reference that the caller had, because your reference is only a copy:

var foo = {'bar': 1};

function tryToMungeReference(obj) {
  obj = {'bar': 2};  // won't change caller's object
}

function mungeContents(obj) {
  obj.bar = 2;       // changes _contents_ of caller's object
}

tryToMungeReference(foo);
foo.bar === 1;   // true - foo still references original object

mungeContents(foo);
foo.bar === 2;  // true - object referenced by foo has been modified

其他参考

JavaScript 的指针是什么?
备注:我还是觉得js里面是没有指针的,除了this指针,更何况,ES6都没有this了。不过js里面绝对有比C里面的指针更刺激的东西,见如下系列。

汤姆大叔的博客-深入理解JavaScript系列

未完待续

目录
相关文章
|
6月前
|
JavaScript 前端开发
详解js中的this指向
详解js中的this指向
43 0
|
7月前
js-this指向
js-this指向
|
Dart JavaScript 前端开发
JS中函数的this指向(一)
JS中函数的this指向
31 0
|
分布式计算 JavaScript 前端开发
JS中函数的this指向(二)
JS中函数的this指向
41 0
|
3月前
|
JavaScript 前端开发
【面试题】说说JS中的this指向问题
【面试题】说说JS中的this指向问题
|
8月前
|
JavaScript
JS数组
JS数组
35 0
|
8月前
|
移动开发 JavaScript 索引
JS 数组(详细介绍使用)
JS 数组(详细介绍使用)
43 0
|
7月前
|
移动开发 JavaScript 索引
JS 数组
JS 数组
26 0
|
8月前
|
存储 JavaScript 算法
JS之数组
JS之数组
|
8月前
|
JavaScript 前端开发 数据可视化