Skip to content

Instantly share code, notes, and snippets.

@Nifled
Created May 8, 2019 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nifled/5155f5aea5686a55c32a785c1f20af70 to your computer and use it in GitHub Desktop.
Save Nifled/5155f5aea5686a55c32a785c1f20af70 to your computer and use it in GitHub Desktop.
Getter/Setters with ES5/ES6 in TypeScript
// ES5
var Element = (function() {
function Element() {
this._class = null;
}
Object.defineProperty(Element.prototype, 'className', {
get: function() {
return this._class;
},
set: function(name) {
this._class = 'todd-' + name;
},
enumerable: true,
configurable: true,
});
return Element;
})();
//ES6
export class Element {
private _class: string = null;
get className() {
return this._class;
}
set className(name) {
this._class = `todd-${name}`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment