Mootools“无法读取未定义的属性'调用'”
Mootools "Cannot read property 'call" of undefined"
我有一个使用MooTools的网站。我一直得到以下错误,但无法弄清楚是什么原因造成的。我运气很少能追踪到它。有谁知道这可能是什么?我无法在网上找到任何关于它的信息。
未捕获的类型错误:无法读取未定义的 mootools 核心的属性"调用".js:4497
条件 mootools-核心.js:4497
Defn Mootools-core.js:4511
Stackoverflow有这个问题,但据我所知,它并不相关。想法?
更新
再看了一下,错误的原因仍然是神秘的。我的代码中有问题的行是一个 addEvent 调用:
window.addEvent('load', preloader(preload));
变量 preload
是一个图像网址数组。而回调preloader
是预加载preload
中指定的图像的方法。下面是预加载器方法:
/**
* Event callback that preloads images
*/
function preloader(images) {
var img;
if ( images ) {
for (var i=0; i<images.length; i++) {
img = new Image();
img.src = images[i];
}
}
}
错误指定的mootools代码行如下:
addEvent: function(type, fn){
var events = this.retrieve('events', {});
if (!events[type]) events[type] = {keys: [], values: []};
if (events[type].keys.contains(fn)) return this;
events[type].keys.push(fn);
var realType = type,
custom = Element.Events[type],
condition = fn,
self = this;
if (custom){
if (custom.onAdd) custom.onAdd.call(this, fn, type);
if (custom.condition){
condition = function(event){
//error here--> if (custom.condition.call(this, event, type)) return fn.call(this, event);
return true;
};
}
if (custom.base) realType = Function.from(custom.base).call(this, type);
}
var defn = function(){
return fn.call(self);
};
var nativeEvent = Element.NativeEvents[realType];
if (nativeEvent){
if (nativeEvent == 2){
defn = function(event){
event = new DOMEvent(event, self.getWindow());
if (condition.call(self, event) === false) event.stop();
};
}
this.addListener(realType, defn, arguments[2]);
}
events[type].values.push(defn);
return this;
},
您不会将函数作为回调传递。
这:
window.addEvent('load', preloader(preload));
/**
* Event callback that preloads images
*/
function preloader(images) {
var img;
if ( images ) {
for (var i=0; i<images.length; i++) {
img = new Image();
img.src = images[i];
}
}
}
它本质上会立即调用 preloader
函数,而不是onload
- 它将尝试将事件绑定到 preloader
函数的结果,该函数根本不返回任何内容。
当解释器看到preloader(preload)
时,它只是立即运行它。 您可以返回一个函数,或者更好的是,重写为:
window.addEvent('load', function(){ preloader(preload); });
// or even
window.addEvent('load', preloader.bind(this, preload));
运行示例:
var imagesArray = new Array(50).join(',').split(',');
imagesArray = imagesArray.map(function(el, i){
return 'http://dummyimage.com/600x400/000/' + (255 - i) + '?' + +new Date();
});
function preloader(images) {
var img;
if ( images ) {
for (var i=0; i<images.length; i++) {
img = new Image();
img.src = images[i];
console.log(img.src);
}
}
}
window.addEvent('load', function(){
preloader(imagesArray);
});
<script src="//cdnjs.cloudflare.com/ajax/libs/mootools/1.5.0/mootools-core-full-nocompat.js"></script>
您还可以查看我的预加载器类,它为您提供了更大的灵活性,可以预加载图像以及进度 https://github.com/DimitarChristoff/pre-loader 等
。Mootools-more也有您可以使用Asset.images
。
最后,不确定是否要绑定到load
事件(该事件将在加载所有资产(包括图像(时触发(,您应该能够改为从domready
开始。
我相信,这意味着mooTools在您的一个调用中获得了未定义/格式错误的参数。
找出在mootools-core的第4497行定义了哪个函数.js(可以是对象方法(,并在脚本中查找每个调用。记录您正在使用的所有参数/对象,您会发现错误;)
编辑
看到您的代码,我认为您的问题来自以下事实:type
没有在条件中声明。尝试:
if (custom.condition){
condition = function(event,type){
if (custom.condition.call(this, event, type)) return fn.call(this, event);
return true;
};
- 如何将属性值调用到 javascript
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Es6:能够在设置/更新/删除对象属性时调用自定义方法
- 无法从JavaScript中的函数调用对象属性
- 如何在只接受字符串值/文字的HTML属性中调用函数
- 在javascript函数调用中传递的HTML属性不完整
- 无法获取属性'的值;拆分'在IE8中调用ajax之后
- 如何将 JavaScript Yahoo Weather API 调用属性替换为变量
- RefluxJS存储可以在调用trigger()时指示哪个属性已更改吗?
- 通过 Jquery 调用 tr 属性并找到我的内部复选框
- 如何处理 getJSON 调用上的“未捕获类型错误:无法设置未定义的属性'prop'”
- 从设置为原型的对象调用属性
- 使用onclick调用属性对象函数
- 从 HTML 中的外部 JS 文件调用属性
- JavaScript:调用属性'内的私有函数;的事件侦听器
- 在函数JavaScript中调用属性
- 如何在c++中动态调用属性名,就像JS中的方括号符号一样
- 为什么对字符串调用属性不会出错
- Node.js:使用破折号(-)调用属性
- 当你声明/调用属性时,括号是什么意思?