【发布时间】:2014-08-05 21:08:47
【问题描述】:
我想要一个原型并调用一个静态函数。这是我的代码:
function Client() {
this.username = "username",
this.password = "Password",
this.printUsername = function() {
this.username=$("#Username").val()
console.log(this.username);
};
};
这是我的称呼:
<button onclick="Client.printUsername()" type="button">Submit</button>
这是控制台中的错误:
Uncaught TypeError: Object function Client() {
this.username = "username",
this.password = "Password",
this.printUsername = function() {
this.username=$("#Username").val()
console.log(this.username);
};
} has no method 'printUsername'
我的代码有什么问题?
这仍然会产生错误:
function Client() {
this.username = "username",
this.password = "Password",
this.setUsername = function() {
this.username=$("#Username").val()
console.log(this.username);
};
};
Client.prototype.printUsername = function() {
this.username = $("#Username").val();
console.log(this.username);
}
【问题讨论】:
-
你必须实例化你的
Client -
愿意在静态函数中打印带有上下文的用户名的简单事实是设计缺陷。
-
OP 认为他需要静态调用的事实是恕我直言的设计缺陷。 @OP 你知道静态是什么意思吗?
-
OP,您需要重新表述您的需求,我们才能正确回答您的问题。你到底想用 printUsername 做什么,为什么它应该是静态的?
标签: javascript static