我正在设计一个对象,如下所示。但是JavaScript不嵌套callable。我感谢同事们对此的想法。我分享了原始代码。
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--第二个回调未启动
一个想法?
发布于 2015-12-01 21:32:20
您有不同的变量名-您的参数是callback,但您调用的是value。
此外,您还拼写错了Object.defineProperties (preloading.defineProperties)、enumerable (enumarable)和setTimeout(setTimeOut)。
当然,你是在没有回调的情况下调用preloading.hide()的,所以它试图在undefined上调用.call,这也会抛出。
发布于 2015-12-01 22:08:15
试试这个
var preloading = {};
Object.defineProperties(preloading,{
show:{
enumerable:true,
writable:false,
value:function(callback){
var $this = this;
setTimeout(function(){
console.log('show callback starting');
callback.call(undefined, $this);
},500)
}
},
hide:{
enumerable:true,
writable:false,
value:function(callback){
var $this = this;
setTimeout(function(){
console.log('hide callback starting');
callback.call(undefined, $this);
},500)
}
}
});您使用preloading.defineProperties(....);而不是Object.defineProperties(....);,使用setTimeOut而不是setTimeout,这可能是问题所在。
https://stackoverflow.com/questions/34021636
复制相似问题