如果我跑定义之前,下面的功能,我会得到这个错误...在调用它之前是否需要定义一个Javascript函数?
Uncaught ReferenceError: openModal is not defined
运行,然后定义
$(document).ready(function() {
delay(openModal, 2000);
delay = function (f, t) {
setTimeout(function() {
f();
}, t);
};
openModal = function() {
$('#modal-box').css({
left: $(window).width()/2 - $('#modal-box').width()/2,
top: $(window).height()/2 - $('#modal-box').height()/2
});
$('#modal-box').show();
$('#modal-mask').show();
};
});
现在,如果我先定义函数,然后调用它,它的工作原理...我在PHP中有一个背景,所以我习惯于能够全局访问函数,我是否做错了什么,或者在使用之前必须定义所有函数?
$(document).ready(function() {
delay = function (f, t) {
setTimeout(function() {
f();
}, t);
};
openModal = function() {
$('#modal-box').css({
left: $(window).width()/2 - $('#modal-box').width()/2,
top: $(window).height()/2 - $('#modal-box').height()/2
});
$('#modal-box').show();
$('#modal-mask').show();
};
delay(openModal, 2000);
});
是一切,即你声明的标识符'openModal'(而不是限定它)上面范围?例如'var openModal;' – Rup 2012-04-02 08:59:04
相关:http://stackoverflow.com/questions/261599/why-can-i-use-a-function-before-its-defined-in-javascript – TJHeuvel 2012-04-02 08:59:12
@Rup这是第一次openModal被使用 – JasonDavis 2012-04-02 08:59:55