我正在设计一个对象,如下所示。但 JavaScript 没有嵌套可调用。我感谢同事们提出了一个想法。我分享原始代码。
var preloading = {};
Object.defineProperties(preloading,{
show:{
enumarable:true,
writable:false,
value:function(value){
var $this = this;
jQuery($this._object).find('.text').eq(0).html($this.text).end().end()
.velocity('stop').velocity('fadeIn',{
duration:500,
complete:function(){
console.log('preloading show');
if(value instanceof Function || typeof value === 'function'){
$this._start(value);
}
}
});
}
},
hide:{
enumarable:true,
writable:false,
value:function(value){
var $this = this;
jQuery($this._object).velocity('stop').velocity('fadeOut',{
duration:500,
complete:function(){
console.log('preloading hide');
if(value instanceof Function || typeof value === 'function'){
$this._finish(value);
}
}
});
}
},
_start:{
enumerable:false,
writable:false,
value:function(value){
var $this = this;
value.call(undefined, $this);
}
},
_finish:{
enumerable:false,
writable:false,
value:function(value){
var $this = this;
value.call(undefined, $this)
}
},
_object:{
writable:true,
value:'#preloader2',
enumarable:false
},
object:{
get:function(){
return this._object;
},
set:function(value){
this._object = value;
}
},
_text:{
writable:true,
value:'yükleniyor..',
enumerable:false
},
text:{
get:function(){
return this._text;
},
set:function(value){
this._text = value;
}
}
});
那我试试
preloading.show(function(preloading){preloading.hide()})
--第一次回调开始
//show callback starting
--第二个回调未开始
有什么想法吗?
最佳答案
您有不同的变量名称 - 您的参数是callback
,但您正在调用value
。
此外,您还拼写错误了 Object.defineProperties
(preloading.defineProperties
)、enumerable
(enumarable
) 和setTimeout
(setTimeOut
)。
当然,您在没有回调的情况下调用 preloading.hide()
,因此它会尝试在 undefined
上调用 .call
,其中也会抛出。
关于javascript 对象嵌套回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34021636/