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 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系列

未完待续

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值