简体   繁体   English

覆盖传递给函数的对象

[英]Overriding an object that was passed to a function

I was wondering recently if this is possible: Passing an object to a function and overriding the entire object with a complete different object inside of that function, so that its value is permanently modified. 我最近想知道这是否可能:将一个对象传递给一个函数,并用该函数内部的一个完全不同的对象覆盖整个对象,以便永久修改它的值。 Since objects are passed by reference, I have a feeling that it must work somehow but I have no idea how. 由于对象是通过引用传递的,我觉得它必须以某种方式工作,但我不知道如何。 To put it in code, it should do something like this: 要把它放在代码中,它应该做这样的事情:

function a() {
    console.log("i am a");
}

function b() {
    console.log("i am b");
}

function override(func1, func2) {
    func1 = func2;
}

override(a, b);

a(); //"i am b" expected

FIDDLE 小提琴

This obviously doesn't work because I'm just overriding the variable func1 but I think you get the idea. 这显然不起作用,因为我只是覆盖变量func1但我认为你明白了。 And I chose to use functions instead of pure objects, because you could easily "override" an object by just deleting its properties and copying the new properties from the second object. 我选择使用函数而不是纯对象,因为您可以通过删除其属性并从第二个对象复制新属性来轻松“覆盖”对象。 But that's not what I want. 但这不是我想要的。

And before you ask, I'm not trying to solve any real life problem with this, I'm just asking out of curiosity. 在你问之前,我并不想用这个来解决任何现实生活中的问题,我只是出于好奇而问。 So is there a way to achieve this and if not, is there a reason why not? 那么有没有办法实现这一点,如果没有,是否有理由不这样做?

Thanks! 谢谢!

This doesn't work because although JS passes references to functions that just means that your func1 and func2 parameters end up referring to the same functions (objects) that the identifiers a and b refer to but when you change what func1 refers to that doesn't affect what a refers to. 这不起作用,因为虽然JS传递对函数的引用,这意味着你的func1func2参数最终引用了标识符ab引用的相同函数(对象),但是当你改变func1引用的那些函数时牛逼影响什么a指。

To put it another way, when you pass an object to a function the function can change the content of the object by modifying, adding or deleting properties, but it can't change which object a variable outside the function refers to. 换句话说,当您将对象传递给函数时,该函数可以通过修改,添加或删除属性来更改对象的内容,但它不能更改函数外部的变量引用的对象。 (At least, not via the function's own argument - a function can modify variables in its scope chain up to and including global variables, but it has to do so by referring to them by their own names.) (至少,不是通过函数自己的参数 - 函数可以修改其范围链中的变量,直到并包括全局变量,但它必须通过自己的名称引用它们来实现。)

In your code 在你的代码中

function override(func1, func2) {
    func1 = func2;
}

func1 is a variable that points to function a and func2 is a variable that points to function b . func1是指向函数a的变量, func2是指向函数b的变量。 The statement func1 = func2 makes func1 point to b. 语句func1 = func2使func1指向b。 It does not change anything outside the function override . 它不会更改函数override之外的任何内容。

Please use objects to make use of pass by reference . 请使用对象来使用传递参考 The code given below illustrates how it works. 下面给出的代码说明了它的工作原理。

function a() {
    console.log("i am a");
}

function b() {
    console.log("i am b");
}

var object1 = {
   func1:a
};

var object2 = {
   func2:b
}


function override(object1, object2) {
    object1.func1 = object2.func2;
}

override(object1, object2);

object1.func1(); //"i am b"

This code is just for demonstration, might not be useful in a real scenario. 此代码仅用于演示,在实际场景中可能没有用。

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

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