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 useDefineProperty
onwindow
. – 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
2 Answers
Reset to default 3It 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条)