我有以下功能:
var appendStructure = {
init : function(wrapper,structure,cls,callback) {
$(wrapper).appendTo(container).hide()
var object = $(container).find(cls);
$(structure.join('')).appendTo(object);
showObj(object,function() {
if(opts.centerObj == true) {
$(window).resize(function() {
var cssProps = getProps(object);
object.css(cssProps);
});
}
if(typeof callback == 'function') {
callback();
}
});
}
}
以及在其中调用的其他函数:
var getProps = function(obj) {
return {
'position' :'absolute',
'top' : (($(window).height() - $(obj).outerHeight()) / 2)+'px',
'left' : (($(window).width() - $(obj).outerWidth()) / 2)+'px'
}
}
var showObj = function(obj,callback) {
return setTimeout(function () {
if(opts.centerObj == true) {
var cssProps = getProps(obj);
obj.css(cssProps).fadeIn('slow');
}
else {
obj.fadeIn('slow');
}
if(typeof callback == 'function') {
callback();
}
}, 1500);
}
我像这样运行这个函数:
if(appendStructure.init(wrapper.login,structure.login,'.content-login')){
console.log('Object Appended');
}
else {
console.log('Error');
}
我的问题是,为什么控制台输出Error
,因为该函数实际上有效并且所有应该发生的事情都发生了?
最佳答案
appendStructure.init
不return
任何值,因此返回值为 undefined
。 undefined
计算结果为false
,所以else
你的分支if...else
语句被执行。
关于javascript - 函数运行过程中出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8869145/