JavaScript创建对对象属性的引用? [英] Javascript create reference to an object property?

查看:40
本文介绍了JavaScript创建对对象属性的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,在javascript中,基元按值传递,对象按引用传递.

I understand that in javascript, primitives are passed by value and objects are passed by reference.

我对创建某种变通办法很感兴趣,该变通办法将使我能够获取对包含基元的对象属性的引用.例如,我希望可以的工作是:

I'm interested in creating a workaround of some kind that would let me get a reference to an object property containing a primitive. For example, what I wish would work is:

var someObject = {a: 1, b: 2};
var myRef = someObject.b;
myRef ++;
someObject.b #=> 3

当然,这是行不通的.我知道您可以改为创建一个getter和setter函数,或使用一个对象来引用另一个对象,但是我真正想要的是一种变通方法,它允许我定义一个变量来作为对属性的引用.另一个对象,到目前为止,看来这是无法完成的.

Of course, this doesn't work. I'm aware that you could create a getter and setter function instead, or use one object to reference another object, but what I'd really like is some kind of workaround that allowed me to define a variable as a reference to the property of another object, and so far it seems this just can't be done.

所以,我的问题很简单:这是否可能?如果可以,怎么办?

So, my question is simply: is this even possible, and if so, how?

推荐答案

原始类型是不可变的,因此不能,这是不可能的.您可以使用对象包装原始类型,如下所示:

Primitive types are immutable, so no, it's not possible. You can wrap your primitive type with an object, like this:

function MyNumber(n) { this.n = n; }
MyNumber.prototype.valueOf = function() { return this.n; }
var someObject = { a: 1, b: new MyNumber(2) };
var myRef = someObject.b;
MyNumber.call(myRef, myRef + 1);
console.log(+someObject.b); // convert to number with +

OR

var someObject = {
    a: { value: 1 },
    b: { value: 2 },
};
var myRef = someObject.b;
my_inc(myRef); // function my_inc (obj) { obj.value++; }
// someObject.b.value == 3

React框架使用非常简单的模式来封装值.

The React framework uses a very simple pattern to encapsulate values.

function Link(value, requestChange)
{
    this.value = value;
    this.requestChange = requestChange;
}

您可以传递对象,可以通过检查对象的value属性来访问当前值,如果要更改它,可以使用新值调用 requestChange ,可以进行更改价值.这样做的好处是使实际的存储位置"和更改值的逻辑与值的读写访问脱离.请注意,这些值也可以是复杂的对象.

You can pass around the object, the current value can be accessed by inspecting the value property of the object, if you want to change it you can call requestChange with a new value, you can change the value. The advantage would be to have the actual "storage location" and the logic for changing the value decoupled from the value read and write access. Note that the values can also be complex objects.

您也可以通过闭包实现类似的目的:

You could also achieve something similar with closures:

var someObject = {
    a: 1,
    b: 2
};

function property(object, prop) {
    return {
        get value () {
            return object[prop]
        },
        set value (val) {
            object[prop] = val;
        }
    };
}

var ref = property(someObject, "b");
ref.value; // 2
++ref.value; // 3
someObject.b; // 3

之所以可行,是因为getter和setter函数在创建时可以访问范围内的任何绑定( object prop ).现在,您可以传递 ref ,将其存储在数据结构中,等等.

This works because the getter and setter functions have access to whatever bindings were in scope at the time of their creation (object and prop). You can now pass ref around, store it in a data structure, etc.

这篇关于JavaScript创建对对象属性的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆