简体   繁体   English

访问/修改函数内部的对象| javascript

[英]access/modify an object inside of a function | javascript

lets say I have a function - object constructor: 可以说我有一个函数-对象构造函数:

var constructor = function(name, surname, town){
    return {
        Name: name,
        Surname: surname,
        Town: town
    }
};

Now I can create a new obj like this: 现在,我可以像这样创建一个新的obj:

var peter = constructor("Peter", "Jameson", "London");

and I can add new properties to my new object peter using dot notation like this: 我可以使用点符号将新属性添加到我的新对象peter如下所示:

peter.phoneNumber = 856687;

My question is: Is it possible to add more properties to my constructor using dot/bracket notation when the object in the constructor/function has no var? 我的问题是:当构造函数/函数中的对象没有var时,是否可以使用点/括号符号为构造函数添加更多属性?

It sounds like you're trying to modify the behavior of constructor at runtime. 听起来您正在尝试在运行时修改constructor的行为。 You've said: 您已经说过:

I would like to modify the code of constructor using dot notation (if its possible in this case), eg I would like to add more properties to my constructor. 我想使用点符号(如果在这种情况下可能的话)修改构造函数的代码,例如,我想向构造函数添加更多属性。 If the code was like this: 如果代码是这样的:

 var constructor = function(name, surname, town){ var person = { Name: name, Surname: species, Town: town }; return person; }; 

...then I guess I could modify my constructor like this: {constructor.person.phoneNumber = 554457; ...那么我想我可以这样修改我的构造函数: {constructor.person.phoneNumber = 554457; but in my original code constructor has no var inside so I cant taget obj 但是在我的原始代码构造函数中没有var,所以我无法标记obj

As @deceze pointed out, no, you couldn't do that even if constructor had a variable in it; 正如@deceze所指出的,不,即使constructor中包含变量,您也无法执行此操作。 that variable is entirely private to the function, it's not exposed as a property of constructor . 该变量完全是该函数的私有变量,不会作为constructor的属性公开。

The only way 1 to modify constructor 's behavior at runtime is to wrap it in a new function, like this: 修改constructor 在运行时的行为的唯一方法1是将其包装在新函数中,如下所示:

(function() {
    var original = constructor;
    constructor = function() {
        var obj = original.apply(this, arguments);
        obj.phoneNumber = 554457;
        return obj;
    };
})();

That calls the original version with all of the arguments it receives (and the same this ), then adds the additional property, and returns it. 这要求原始版本与所有收到的参数(和相同的this ),然后添加附加属性,并将其返回。


1 Okay, technically , you could decompile it with toString (on browsers that support it; support is now required as of ES5, so it's pretty good), use text manipulation to change its contents, then use new Function to turn it back into a function and assign the result to constructor , but A) It's a really bad idea, and B) The function would lose its context, and so if it relied on closing over any variables that aren't globals, it would stop working (hence [A]). 1好吧,从技术上讲 ,您可以使用toString对其进行反编译(在支持它的浏览器上;从ES5开始需要支持,因此非常好),使用文本操作来更改其内容,然后使用new Function将其转换为函数并将结果分配给constructor ,但是A)这是一个非常糟糕的主意,B)该函数会丢失其上下文,因此,如果它依赖于关闭所有非全局变量,它将停止工作(因此[一种])。

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

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