Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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 OOP和jQuery:如何使用Deferred()初始化变量;_Javascript_Oop_Jquery Deferred - Fatal编程技术网

Javascript OOP和jQuery:如何使用Deferred()初始化变量;

Javascript OOP和jQuery:如何使用Deferred()初始化变量;,javascript,oop,jquery-deferred,Javascript,Oop,Jquery Deferred,这是我的javascript代码 它的目标只是为了教育。我正在学习JSOOP和jquery function App() { this.deviceReadyDeferred = new $.Deferred(); this.init = function() { console.log ("Run"); $.when(this.deviceReadyDeferred).then(this.run); document.addEventListener("click"

这是我的javascript代码 它的目标只是为了教育。我正在学习JSOOP和jquery

function App() {

this.deviceReadyDeferred = new $.Deferred();

this.init = function() {
    console.log ("Run");
    $.when(this.deviceReadyDeferred).then(this.run);
    document.addEventListener("click", this.onDeviceReady, false);

},

// NB: onDeviceReady non ha parametri evento
this.onDeviceReady = function() {
    console.log("deviceReady");
    this.deviceReadyDeferred.resolve();
},

this.run = function() {
    console.log("main");
}

}

app = new App();
app.init();
当我点击时,我收到

TypeError:this.DeviceRadyFerred未定义

为什么?

  • 我没有收到未定义的“$”命令,因此jQuery运行良好
  • 我正在Win7上运行基于FF 19.0.2的jQuery 1.9.1
如何在javascript对象中使用延迟?如何初始化和重用它

编辑:

此代码正在运行。 所有的问题都是我误用了
这个
。我是javascript面向对象编程的新手

function App() {

    var self = this;

    this.deviceReadyDeferred = new $.Deferred();

    this.init = function() {
        console.log ("Run");
        $.when(self.deviceReadyDeferred).then(self.run);
        $(document).on("click", self.onClick);

    },

    this.onClick = function() {
        console.log("deviceReady");
        self.deviceReadyDeferred.resolve();
    },

    this.run = function() {
        console.log("main");
    }

}

app = new App();
app.init();

this.onDeviceReady = function() {
    ...
}
与它之外的此
不同。jQuery通过将数据传递到事件处理程序中,有一种内置的方法来解决这个问题

function App() {

    this.deviceReadyDeferred = new $.Deferred();

    this.init = function () {
        console.log("Run");
        $.when(this.deviceReadyDeferred).then(this.run);
        $(document).on("click", { App: this }, this.onDeviceReady);
    },

    // NB: onDeviceReady non ha parametri evento
    this.onDeviceReady = function (e) {
        console.log("deviceReady");
        e.data.App.deviceReadyDeferred.resolve();
    },

    this.run = function () {
        console.log("main");
    }

}

app = new App();
app.init();

或者,如果您不必支持IE8,则更容易使用本机方法(注意
.bind(this)
):


其他答案已经解释了原因,这是回调中的
this
的值。解决此问题的方法之一是创建一个绑定到特定
值的新函数:

document.addEventListener("click", this.onDeviceReady.bind(this), false);

如果本机不可用,则需要一个。

正如Kevin B所说,
单击处理程序中的
文档
,因为您首先在
文档
上绑定了该事件

解决这种情况的一种简单方法是使用:


您正在正确使用jQuery及其延迟。您在
this.ondevicerady=function()的
this
内部遇到问题{
与它之外的
不同。@AndrewHitaker您可能没有单击文档触发错误,我正在使用addEventListener,因为稍后该代码将成为phoneGap 2.5 android应用程序的一部分,并且它已经是一个工作代码!+1,$。代理比绑定好,因为它是jQuery免费提供的。是的!我编辑了m我的问题是复制/粘贴了一个已经测试过的版本。在FF 19上测试过真的吗?请告诉我什么样的错误?我发布了一个代码的工作版本。刚刚再次测试并工作了。我认为@bfavaretto暗示了以下事实:IE@realtebo事实上,我希望你得到同样的“this.deviceradydeered未定义”当您单击文档时。如果您仅更改此答案所建议的内容,您仍然会看到该错误。但是我从您的更新中看到,您更改得更多,您在更高的范围内定义了
self
,并在
ondevicerady
中将
this
替换为
self
。这就是它工作的原因。@Beetroot Beetroot他发表了上述评论。
this.init = function() {
    console.log ("Run");
    var self = this;
    $.when(this.deviceReadyDeferred).then(self.run);
    document.addEventListener("click", self.onDeviceReady, false);

},
document.addEventListener("click", this.onDeviceReady.bind(this), false);
document.addEventListener("click", $.proxy(this.onDeviceReady, this), false);