angularjs - Can I create a javascript function in EcmaScript 5 with the new get and set in one declaration? -
i interested in es5 getters , setters use angular.js controllers. doing:
var helloec5 = function(){ //constructor this.pants = "jeans"; }; helloec5.prototype = { firstname: 'seeya', lastname: 'latir', fullname() { console.log("get") return this.firstname + ' ' + this.lastname; }, set fullname (name) { console.log('set') var words = name.tostring().split(' '); this.firstname = words[0] || ''; this.lastname = words[1] || ''; } };
but there way in function() succinctly together? want (pseudo code) ;
var helloec5 = function() { firstname: 'seeya', lastname: 'latir', fullname() { console.log("get") return this.firstname + ' ' + this.lastname; }, set fullname (name) { console.log('set') var words = name.tostring().split(' '); this.firstname = words[0] || ''; this.lastname = words[1] || ''; } };
you can object.defineproperty() method (http://jsfiddle.net/ydhlbwg6/):
var helloec5 = function () { this.firstname = 'seeya'; this.lastname = 'latir'; object.defineproperty(this, 'fullname', { get: function () { console.log('get'); return this.firstname + ' ' + this.lastname; }, set: function (value) { console.log('set'); var words = value.tostring().split(' '); this.firstname = words[0] || ''; this.lastname = words[1] || ''; } }); };
Comments
Post a Comment