Created
May 8, 2019 22:10
-
-
Save Nifled/5155f5aea5686a55c32a785c1f20af70 to your computer and use it in GitHub Desktop.
Getter/Setters with ES5/ES6 in TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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