gpt4 book ai didi

javascript - isFlag 而不是 isFlag() 语法 : is it possible in Javascript

转载 作者:行者123 更新时间:2023-11-30 07:02:06 24 4
gpt4 key购买 nike

<分区>

在下面的代码中,我定义了 Greeter.prototype.isVeryPolite = function () {... 用于访问 this._isVeryPolite

greeter.isVeryPolite()

但是最后的“()”不是很友好。 javascript 中是否有技巧可以在不直接访问成员的情况下拥有 greeter.isVeryPolite?

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);

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com