Javascript getters and setters for object, not object properties - Stack Overflow

I know how to use JS getters and setters for object properties like sovar myObject = {value : 0,get pro

I know how to use JS getters and setters for object properties like so

var myObject = {
    value : 0,
    get property() {
        return this.value;
    },
    set property(v) {
        this.value = v;
    }
}

so that calling myObject.property = 2 will set myObject.value, but what I'm wondering is if there is some way to call myObject = 2 and still set myObject.value rather than changing myObject from an object into a number.

It's probably not possible, but javascript is an incredibly flexible language and I thought I'd pose the question to the munity before I discarded the idea.

I know how to use JS getters and setters for object properties like so

var myObject = {
    value : 0,
    get property() {
        return this.value;
    },
    set property(v) {
        this.value = v;
    }
}

so that calling myObject.property = 2 will set myObject.value, but what I'm wondering is if there is some way to call myObject = 2 and still set myObject.value rather than changing myObject from an object into a number.

It's probably not possible, but javascript is an incredibly flexible language and I thought I'd pose the question to the munity before I discarded the idea.

Share Improve this question asked Aug 7, 2014 at 19:14 wackozackowackozacko 7021 gold badge9 silver badges19 bronze badges 6
  • 1 Discard this idea. myObject is a reference, and reassigning it to something else will... well, reassign it to something else. – Dave Newton Commented Aug 7, 2014 at 19:15
  • It doesn't seen to be possible in JavaScript, although internally there's a DefineOwnProperty method, specified in the specification, which is only used when creating arguments for functions. The closest thing you can get is to use DefineProperty on window. – Derek 朕會功夫 Commented Aug 7, 2014 at 19:22
  • 3 I think what you're really after here is overloading the assignment operator, which has been discussed before – Don Commented Aug 7, 2014 at 19:22
  • @DaveNewton you can define getters and setters so when a reference is being assigned, it only executes the setter function and not actually assigning value to that property. – Derek 朕會功夫 Commented Aug 7, 2014 at 19:25
  • @Derek朕會功夫 That's pretty different than setting an arbitrary reference to something other than what you're setting it to. Or rather, it's different than making the assignment operator do something other than assign. This is baked into the language. – Dave Newton Commented Aug 7, 2014 at 19:28
 |  Show 1 more ment

2 Answers 2

Reset to default 3

It is possible indeed. Only for global variables though.

Object.defineProperties(this, {
    myObject: {
        get: function () {
            return myObjectValue;
        },
        set: function (value) {
            myObjectValue = value;
        },
        enumerable: true,
        configurable: true
    },
    myObjectValue: {
        value: 0,
        enumerable: false,
        configurable: true,
        writable: true
    }
});

myObject = 5;
console.log(myObject);
console.log(delete myObject);

Now, every time you assign a value to myObject, it shall actually run the set function and assign the value to the other property instead. Now, if you wanted to minimize pollution, you could create an IIFE and use variables inside that instead to hold the values, per se.

http://jsbin./zopefuvi/1/edit

And here is the version with the IIFE.

http://jsbin./puzopawa/1/edit

Just as a side note. I didn't know until reading this question that you could define getters/setters without Object.defineProperty. I was wondering at what the difference was between this 'shorthand' method, and defining getters/setters via Object.defineProperty.

I found, using the myObject example in the question:

var myObject = {
    value : 0,
    get property() {
        return this.value;
    },
    set property(v) {
        this.value = v;
    }
}

that Object.getOwnPropertyDescriptor(myObject, 'property')

returns

get: ƒ property()
set: ƒ property(v)
enumerable: true
configurable: true

So worth mentioning is that property in this case is enumerable, meaning it will show up in loops.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745065636a4609238.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信