简体   繁体   English

是否可以通过引用传递或访问实际对象?

[英]Is it possible to pass-by-reference or access the actual object?

I understand that javascript is always "Pass-by-value". 我了解javascript始终是“按值传递”。 Even when when assigning an object, it passes a value which is a reference to the object. 即使在分配对象时,它也会传递一个 ,该是对该对象的引用。

1: var obj1 = {name: "bob"};
2: var obj2 = obj1;
3: obj2.name = "joe";
4: console.log(obj1.name);    // output: "joe"
5: obj2 = {name: "ron"};
6: console.log(obj1.name);    // output: "joe"

On line 4, both obj1 and obj2 contain a reference to the same object, and that we are able to manipulate that object through both variables. 在第4行, obj1obj2都包含对同一对象的引用,并且我们能够通过两个变量来操纵该对象。 But line 5 sets obj2 to refer to a new object. 但是第5行将obj2设置为引用新对象。 now obj1 and obj2 have references to 2 different objects. 现在obj1obj2引用了2个不同的对象。 This all makes sense so far. 到目前为止,所有这些都是有道理的。

My question is this: is there any way to access the object itself and not just references to it? 我的问题是:有什么方法可以访问对象本身,而不仅仅是对其的引用? So that instead of simply manipulating the object we can change it to a whole new object and any variables that referenced it would now reference the new object? 这样,我们可以将其更改为一个全新的对象,而不是简单地操作该对象,现在引用该对象的任何变量都将引用该新对象? something like this: 像这样的东西:

var obj1 = {name: "bob"};
var obj2 = obj1;
var actualObject = GetActualObjectReferencedBy(obj2);
actualObject = new Date();
console.log(obj1)      // output: the current date;
console.log(obj2)      // output: the current date;

So that actualObject is just that. 所以那个actualObject就是这样。 the actual object that everything is referring to, not just a reference. 一切所指的实际对象,而不仅仅是参考。 And making ANY changes to it doesn't break the references. 对其进行任何更改都不会破坏引用。

Here is an example of why I want to do this: 这是我为什么要这样做的一个示例:

function Person(n){
  this.name = n;
}
function attachProxies(){
  for(var i = 0; i < arguments.length; i++){
    var actualObject = GetActualObject(arguments[i]);
    actualObject = new Proxy(actualObject, {
      get: function(obj, prop){ return "I'm " + obj[prop]; };
    });
  }
}

var guy1 = new Person("bob");
var guy2 = new Person("joe");
var guy3 = new Person("rom");
attachProxies(guy1, guy2, guy3);

console.log(guy2.name);    // output: "I'm joe"

The closest thing I can figure is this: 我能想到的最接近的东西是:

function attachProxies(obj){
  return new Proxy(obj, {
    get: function(obj, prop){return "I'm "+obj[prop];};
  });
}
var guy1 = new Person("bob");
var guy2 = new Person("joe");
var guy3 = new Person("rom");
guy1 = attachProxies(guy1);
guy2 = attachProxies(guy2);
guy3 = attachProxies(guy3);

But that's much more limited and repetitive. 但这是更加有限和重复的。

I've tried looking around but I can't seem to find any way of doing this. 我曾尝试环顾四周,但似乎找不到任何方法。 If it's not possible, please help me understand the reasoning. 如果不可能,请帮助我理解原因。

Edit: 编辑:

I don't believe this counts as a duplicate because I acknowledge in the setup to my question that javascript passes by value. 我不认为这是重复的,因为我在设置问题中承认javascript按值传递。 The supposed duplicate only states that but does not answer my question of whether there is any way access the object. 假定的重复项仅声明但未回答我是否有任何方式访问对象的问题。

No, this is not possible. 不,这是不可能的。 You cannot get access to the memory where the object's contents are stored and replace them with different contents taken from another object. 您无法访问存储对象内容的内存,并用取自另一个对象的不同内容替换它们。

Well, you can do that, but in a limited way. 好吧,您可以这样做,但要有一定的限制。 You can change the object's contents, which are mainly the object's properties , by simply assigning them new values. 您可以通过简单地为其分配新值来更改对象的内容,这些内容主要是对象的属性 You can even swap out its prototype chain with Object.setPrototypeOf . 您甚至可以使用Object.setPrototypeOf换出其原型链。 (Assuming that the object isn't frozen or otherwise non-configurable). (假设对象没有冻结或无法配置)。

But what you absolutely cannot do is swap out the object for an object of a different internal type, such as your plain object-literal object for a Date instance of even a Proxy . 但是,您绝对不能做的是将对象换成其他内部类型的对象,例如,将普通的纯文字对象替换为ProxyDate实例。 This is fundamentally impossible. 从根本上讲这是不可能的。

Your "the closest thing" solution is indeed the closest thing you can do, and it involves assigning new reference values to all the places (in your case, variables) that stored the old reference. 您的“最接近的事物”解决方案确实是您可以执行的最接近的事情,它涉及为存储旧参考的所有位置(在您的情况下为变量)分配新的参考值。 You can however make it less repetitive if your wrapper function supports the syntax 但是,如果包装函数支持以下语法,则可以减少重复性

[guy1, guy2, guy3] = attachProxies(guy1, guy2, guy3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM