我有这个方法:
var stopwatch = function () {
this.start = function () {
(...)
};
this.stop = function() {
(...)
};
};
当我尝试调用它时:
stopwatch.start();
我收到Uncaught TypeError: Object (here is my function) has no method 'start'
。我做错了什么?
最佳答案
当函数 stopwatch
运行时,您将函数分配给 this.start
和 this.stop
从不运行该函数。
看起来您想要一个带有一些原型(prototype)的构造函数。
// By convention, constructor functions have names beginning with a capital letter
function Stopwatch () {
/* initialisation time logic */
}
Stopwatch.prototype.stop = function () { };
Stopwatch.prototype.start = function () { };
// Create an instance
var my_stopwatch = new Stopwatch();
my_stopwatch.start();
关于JavaScript - 未捕获的类型错误 : Object has no method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19053341/