javascript get / set for property/ values -


why dose log 3 twice ? , how should done?

so can set property car.set('property', 'value')

and return value car.get('property')

for value / property

function model() {}    model.prototype = {    get: function(property) {      return this._value;    },    set: function(property, value) {      this.property = value;      this._value = value;    }  };    var car = new model();    car.set('name', 'ford');  car.set('age', 3);      console.log(    car.get('name'),    car.get('age')  );

since use dot syntax set properties, property names "property" , "_value". if set multiple values, last 1 overwrite previous one.

instead, think wanted set property name value in property variable. can use bracket syntax achieve that.

model.prototype = {   get: function(property){       return this[property];   },   set: function (property, value){         this[property] = value;     } }; 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -