Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过异步调用访问另一个对象原型上的对象_Javascript_Oop_Prototype - Fatal编程技术网

Javascript 通过异步调用访问另一个对象原型上的对象

Javascript 通过异步调用访问另一个对象原型上的对象,javascript,oop,prototype,Javascript,Oop,Prototype,我有一个异步调用,当它完成时,我想在父对象上设置一个属性 参加以下课程,cat: var cat = function(){ this.isLoaded = false; this.image = new Image(); this.image.onload = function(){ //how can I set the isLoaded variable above from here?? } this.image.src = "..."; } 我

我有一个异步调用,当它完成时,我想在父对象上设置一个属性

参加以下课程,cat:

var cat = function(){
   this.isLoaded = false;
   this.image = new Image();
   this.image.onload = function(){
     //how can I set the isLoaded variable above from here??
   }
   this.image.src = "...";
}
我希望能够做到:

var myCat = new cat();
if(myCat.isLoaded)
....
我似乎不知道如何在onload中设置上面的isLoaded属性。我可以通过“isLoaded”访问它,但我认为我是通过值而不是引用来访问它的,因此无法更改它

这似乎是一个简单的解决方案,我想我缺乏更深层次的理解


谢谢

onload
回调中,
isLoaded
undefined
,如果您设置它,它将是
窗口。isLoaded
(基本上是全局的)

回调函数的
this
也指向
img
元素,而不是它的父函数。我制作了一个
that
变量,该变量指向外部函数的
this
。函数中的函数可以在JavaScript中访问其外部函数的作用域

var Cat = function(){
   this.isLoaded = false;
   this.image = new Image();
   var that = this;
   this.image.onload = function(){
     that.isLoaded = true;
   }
   this.image.src = "http://www.gravatar.com/avatar/5e28492984e3056904e295c43337655f?s=32&d=identicon&r=PG";
}

var c = new Cat();

console.log(c.isLoaded); // false

setTimeout(function() { console.log(c.isLoaded) }, 1500); // true

当然,
setTimeout()
仅用于本例。如果你正在加载一个映像,你应该依赖回调,因为确定映像是否已加载的唯一其他方法是轮询——这不是一个很好的接口

您应该提供一个传入回调的位置,这样您就可以

var c = new Cat(function() {
   // Image loaded, proceed.
});

onload
回调中,
isLoaded
undefined
,如果您设置它,它将是
窗口。isLoaded
(基本上是全局的)

回调函数的
this
也指向
img
元素,而不是它的父函数。我制作了一个
that
变量,该变量指向外部函数的
this
。函数中的函数可以在JavaScript中访问其外部函数的作用域

var Cat = function(){
   this.isLoaded = false;
   this.image = new Image();
   var that = this;
   this.image.onload = function(){
     that.isLoaded = true;
   }
   this.image.src = "http://www.gravatar.com/avatar/5e28492984e3056904e295c43337655f?s=32&d=identicon&r=PG";
}

var c = new Cat();

console.log(c.isLoaded); // false

setTimeout(function() { console.log(c.isLoaded) }, 1500); // true

当然,
setTimeout()
仅用于本例。如果你正在加载一个映像,你应该依赖回调,因为确定映像是否已加载的唯一其他方法是轮询——这不是一个很好的接口

您应该提供一个传入回调的位置,这样您就可以

var c = new Cat(function() {
   // Image loaded, proceed.
});

比如@alex说你需要引用你的猫对象

但是如果你的网络连接很差,设置超时对你没有多大帮助。 下面是一个使用回调功能而不是setTimeout的示例

var cat = function (callback) {
   var this_cat = this;
   this.isLoaded = false;
   this.image = new Image();
   this.image.onload = function(){
     //how can I set the isLoaded variable above from here??
    this_cat.isLoaded = true;
    if (callback instanceof Function) { //@alex tip: callback instanceof Function is more accurate. Chrome reports the regex literal is a function.
        callback();
    }
   }
   this.image.src = "/images/stand_logo.png";
}

var myCat = new cat(function () {
    console.log('cat loaded');
});

比如@alex说你需要引用你的猫对象

但是如果你的网络连接很差,设置超时对你没有多大帮助。 下面是一个使用回调功能而不是setTimeout的示例

var cat = function (callback) {
   var this_cat = this;
   this.isLoaded = false;
   this.image = new Image();
   this.image.onload = function(){
     //how can I set the isLoaded variable above from here??
    this_cat.isLoaded = true;
    if (callback instanceof Function) { //@alex tip: callback instanceof Function is more accurate. Chrome reports the regex literal is a function.
        callback();
    }
   }
   this.image.src = "/images/stand_logo.png";
}

var myCat = new cat(function () {
    console.log('cat loaded');
});

我仅在本例中使用了
setTimeout()
。要确定回调是否是函数,函数的
callbackinstanceof function
更准确。->Chrome报告regex literal是一个函数。我仅在本例中使用了
setTimeout()
。要确定回调是否是函数,
callbackinstanceof function
更准确。->Chrome报告regex literal是一个函数。为了确保我正确理解这一点,函数中的函数可以访问外部函数/对象的“local/private”变量(通过var a声明),而不是附加到函数本身的对象?(通过this.a)@Steve它可以访问其父作用域,但是
this
的值发生了变化,因此您需要一个引用。好的,为了确保我正确理解这一点,函数中的函数可以访问外部函数/对象的“局部/私有”变量(通过var a声明),而不是附加到函数本身的对象?(通过this.a)@Steve It可以访问其父作用域,但是
this
的值发生了变化,因此您需要一个引用。