In the code below I have defined Greeter.prototype.isVeryPolite = function () {... for accessing this._isVeryPolite with
greeter.isVeryPolite()
but the "()" at the end is not very user friendly. Is there a trick in javascript to be able to have greeter.isVeryPolite without accessing a member directly ?
https://jsfiddle.net/5r4so2Ld/
var Greeter = (function () {
function Greeter(message, flag) {
this._name = message;
this._isVeryPolite = flag;
}
Greeter.prototype.greet = function () {
if (this._isVeryPolite) {
return "How do you do, " + this._name;
}
else {
return "Hello " + this._name;
}
};
Greeter.prototype.isVeryPolite = function () {
return this._isVeryPolite;
};
return Greeter;
})();
var greeter = new Greeter("world", true);
var button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function () {
alert(greeter.greet());
alert(greeter.isVeryPolite());
};
document.body.appendChild(button);