Creating multiple properties with the same getters and setters

Problem

I recently wrote some JavaScript program that includes the use of getters and setters . I read the MDN documentation for both methods, but I got confused trying to use them.

In a nutshell, I just want to create a series of similar properties that will have the same getters and setters , but I do not want to rewrite each setter and getter for each property.

Implementation

The above, I tried to execute the following code:

var defaultProperty = { set: function(val) { this.value = val - 1; // Just an example }, get: function() { return this.value + 1; // Just an example } }; var myObj = {}; Object.defineProperties(myObj, { foo: defaultProperty, bar: defaultProperty }); 

Then I assigned new values ​​to my foo and bar properties, for example:

 myObj.foo = 3; myObj.bar = 7; 

And finally, I was expecting something like this :

 console.log(myObj.foo, myObj.bar); > 3 7 

But I unexpectedly got this instead :

 > 7 7 

It seems that both properties apply to either the same memory address or the same setters / receivers. Noticed this, I tried to solve the problem and created each property separately, for example:

 Object.defineProperties(myObj, { foo: { set: function(val) { this.value = val - 1; }, get: function() { return this.value + 1; } }, bar: { set: function(val) { this.value = val - 1; }, get: function() { return this.value + 1; } } }); 

But the result was the same:

 myObj.foo = 3; myObj.bar = 7; console.log(myObj.foo, myObj.bar); > 7 7 

I also tried using the __defineSetter__ and __defineGetter__ , but the result did not change.

My questions

Now, after several unsuccessful attempts to solve my problems, I wonder:

  • Is it possible to define the same setters and getters for different properties of an object?
  • If so, what am I doing wrong, and why do my properties behave as if they are the same property?
  • Is there a better way to accomplish what I'm trying to do (perhaps without writing each setter and getter for each property)?
+6
source share
1 answer

Is it possible to define the same setters and getters with different properties of an object?

Yes, although this is not recommended, as you can see from your experience.

what am I doing wrong, and why do my properties behave as if they are the same property?

Since they store / get the available value that was set / received, it is always stored in the same .value property on your object: myObj.value == 6 , so both myObj.foo and myObj.bar give 7 .

Is there a better way to accomplish what I'm trying to do?

Save the values ​​in the variables of the closing area and use the function to define the properties:

 function makeDefaultProperty(obj, name) { var value; return Object.defineProperty(obj, name, { set: function(val) { value = val - 1; // Just an example }, get: function() { return value + 1; // Just an example } }); }; var myObj = {}; makeDefaultProperty(myObj, "foo"); makeDefaultProperty(myObj, "bar"); 

Of course, instead of a local variable, you can simply use different "internal" properties, and you can also use a different way to create shared setters / getters using a function. Both apply:

 function defaultProperty(name) { return { set: function(val) { this[name] = val - 1; // Just an example }, get: function() { return this[name] + 1; // Just an example } }; } var myObj = Object.defineProperties({}, { foo: defaultProperty("_foo"), bar: defaultProperty("_bar") }); 
+10
source

Source: https://habr.com/ru/post/982919/


All Articles