Johannes
Johannes

Reputation: 6419

Clarifying Javascript Object.defineProperties usage

I still don't feel like I completely understand the use of Object.defineProperties, so I'd appreciate some input on the following (somewhat contrived) examples:


1. my_val behaves the same for one and two in the following code:

var Class_1 = function (arg) {
    this.my_val = arg;
};

var Class_2 = function (arg) {
    Object.defineProperties(this,{
        my_val : {
            value : arg,
            writable : true,
            enumerable : true,
            configurable : true
        }
    });
};

var one = new Class_1("foo");
var two = new Class_2("foo");

And the reasoning for using defineProperties over just assigning it like in Class_1 would be when you want writable, enumerable or configurable to be false; Basically it's for control. Otherwise the simple this.property definition is identical, correct?


2. my_val behaves the same for three and four in the following code:

var Class_value = function (arg) {
    Object.defineProperties(this, {
        my_val : {
            value : arg, 
            writable : true,
//          enumerable : false   // default
//          configurable : false // default
        }
    });
};

var Class_getSet = function (arg) {
    Object.defineProperties(this, {
        __my_val : {
            value : arg,
            writable : true,
//          enumerable : false   // default
//          configurable : false // default
        },
        my_val : {
            get : function(){ return this.__my_val; }, 
            set : function(new_val){ this.__my_val = new_val; }
        }
    });
};

var three = new Class_value("foo");
var four = new Class_getSet("foo");

And the reasoning for using get/set functions over just value would be when you want to add more advanced logic, like calling another function when my_val is updated, or sending an event, etc. Also you cannot use get/set without at least defining the initial value (either via a value property shown here, or via the simple this.property = ... definition as in 1.), correct?


Basically I'm just looking to make sure that I understand the reasoning for how and when to use defineProperties, since I feel like I don't have a good handle on the concept yet.

Upvotes: 0

Views: 80

Answers (1)

Bergi
Bergi

Reputation: 664196

my_val behaves the same for one and two

Yes.

my_val behaves the same for three and four

No - there's __my_val. You might at least consider using a local-scoped, "private" variable instead of a public property.

And the reasoning for using get/set functions over just value would be when you want to add more advanced logic, like calling another function when my_val is updated, or sending an event, etc.

Yes, that's what getters/setters are made for.

Also you cannot use get/set without at least defining the initial value (either via a value property shown here, or via the simple this.property = ... definition as in 1.), correct?

No, you could simply leave it undefined as well.

Upvotes: 1

Related Questions