在Javascript中按值传递和按引用传递 [英] Pass by Value and Pass by Reference in Javascript

查看:54
本文介绍了在Javascript中按值传递和按引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我在Angular2上工作了将近3个月,我遇到了一些情况,这些使我想知道这是怎么发生的?

Now that I've been working on Angular2 for almost 3 months, I've encountered a few scenarios which led me to wonder how does this happen ?

对于特殊的this关键字,很明显它正在传递相关的DOM ObjectClass的引用或实例.

In the case of special this keyword it's obvious that it's passing a reference or instance of the related DOM Object or Class for Example.

但是还有另外一个叫做ControlGroup的东西,每当它分配给另一个变量时,它也会做同样的事情.而且这两个变量都可以访问该ControlGroup的相同实例.

But then there is another thing called ControlGroup which also does the same whenever it's assigned to another variable. And both the variables have access to same instance of that ControlGroup.

推荐答案

对象和数组通过引用传递.原始值(如数字,字符串,布尔值)按值传递.对对象的引用也是一种原始类型,并且像其他原始类型一样通过值进行传递,但是它所引用的对象仍然通过引用进行传递.

Objects and arrays are passed by reference. Primitive values like number, string, boolean are passed by value. A reference to an object is also a primitive type and passed by value like other primitive types, but the object it refers to is still passed by reference.

这不是特定于Angular或TypeScript的,而是JavaScript的工作方式.

This is not Angular or TypeScript specific, just how JavaScript works.

这篇关于在Javascript中按值传递和按引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆