-1

我可以在 Javascript 中做一些自动转换它的东西吗:

var x = 0;

进入以下(但仅在内存中-我不想更改“查看源”):

var x = {Value: 0};

举个例子:

var x = { Value: 0 };

function a(x)    //This function doesn't work if the parameter is not an object, and it would be better if I didn't have to write { Value: 0 }
{
    x.Value++;
}
a(x);
alert(x.Value);
4

2 回答 2

1

这个问题缺乏上下文和细节,但可能会这样做:

function transform(x) {
     return { Value : x };
}

接着

x = transform(x);
于 2012-04-22T09:13:35.870 回答
0

查看所有浏览器的watchObject.watch() 吗?,例如:

var a = function (x) {
    x.value++;
};
var myVariables = {};
myVariables.watch("x", function (prop, oldval, newval) {
                     if (typeof newval !== 'object') {
                         return {"value": newval};
                     };
                 });
myVariables.x = 0;
a(myVariables.x);
alert(myVariables.x.value);
于 2012-04-22T09:12:49.833 回答