Error :Uncaught TypeError: Object function
Error :Uncaught TypeError: Object function
当我写book.main.method();
时,我得到了错误:Uncaught TypeError: Object function () {
window.book = window.book|| {};
book.control = function() {
var check_is_ready = 'check_is_ready';
if(check_is_ready == 'check_is_ready') {
this.startbook = function() {
var bookMain = new book.main();
bookMain.method();
return;
};
};
};
$(function() {
var control = new book.control();
control.startbook();
});
(function () {
book.main = function() {
var index =0;
this.method = function() {
index++;
if (index <= 500){
book.main.method();// <-- this wrong
// the error which I get :Uncaught TypeError: Object function () {
alert (index);
}
};
};
})();
我应该写什么代替book.main.method();
调用它没有错误?
多谢
如果我理解正确的话,主要问题代码是:
(function () {
book.main = function() {
var index =0;
this.method = function() {
index++;
if (index <= 500){
book.main.method();// <-- this wrong
// the error which I get :Uncaught TypeError: Object function () {
alert (index);
}
};
};
})();
您正在尝试递归调用method()
。执行递归调用的另一种方法是为函数表达式指定一个名称(methodFn
)。只要记住这个名字只在函数体中有效:
(function () {
book.main = function() {
var index =0;
this.method = function methodFn() {
index++;
if (index <= 500){
methodFn();
alert (index);
}
};
};
})();
您混淆了构造函数(book.main
)和实例。
this.method = function() {
将一个函数添加到new book.main()
的实例中,而不是添加到book.main
。
我不确定你的最终目标,但你应该替换
book.main.method();// <-- this wrong
this.method();// <-- this works
还请注意,如果您希望看到递增的警报值,您必须切换两行:
(function () {
book.main = function() {
var index =0;
this.method = function() {
index++;
if (index <= 2){
console.log(index); // less painful with console.log than with alert
this.method();
}
};
};
})();
相关文章:
- reducers在redux中得到Function not Object,what'it’他错了
- Angular $q.defer() returns Object{then: function}
- 什么是JavaScript对象.event=object.event=function()
- Function.prototype.propertyname === Object.propertyname is t
- JavaScript 中 Function.prototype 和 Object.prototype 之间的区别
- (Ember.run.throttle)Object Function在Ember.Mixin中没有方法“throttl
- JavaScript Object instanceof Function and Function instanceo
- 如何在javascript中声明成员函数?获取“TypeError: Object.function is not a
- 主干事件不适用于 object.function
- 将 JavaScript Object Function 关联到 HTML 元素
- autoload function vs json object is javascript
- 设计:为什么JavaScript有单独的Function和Object对象,以及为什么它们有自己单独的原型对象
- NodeJs、AngularJs、Mongoose给出错误:TypeError:Object.keys在Function
- Require.js: Uncaught TypeError: Object function ..没有办法
- Node.js TypeError: Object function Object() {[native code]}没
- JavaScript函数语法说明:Function object.myFunction(){.}
- 未捕获的TypeError: object不是"(function($)"上的函数
- Javascript __proto__, Object and Function
- 为什么(Object instanceof Function)和(Function instanceof Object)
- 为什么object.function.this在使用function.call时没有定义