在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.