【问题标题】:Closures for nested anonymous methods in javascriptjavascript中嵌套匿名方法的闭包
【发布时间】:2012-08-06 19:09:49
【问题描述】:

我正在尝试从嵌套匿名 javascript 方法调用的几个级别的深处设置对象(类)级别的变量值。我该怎么做?

这里有一些代码来解释我想要做什么。免责声明:我对 javascript 中的闭包概念不太满意,所以我可能会走错路。任何关于实现我想做的简洁方法的建议将不胜感激。

// FileUtils object.
var FileUtils = function () {
    // Member variables.
    this.ConfRootDir = null;
};

// Method to get a file entry.
// successCallback has to be a method with a FileEntry object.
FileUtils.prototype.getFileEntry = function (fileName, successCallback) {
    if (this.ConfRootDir == null) {
        var thisObj = this;
        // Request the root filesystem 
            // [** 1st callback, using anon method]
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
            function (fileSystem) {                        
                // Get the root directory of the file system.
                var rootDir = fileSystem.root;
                // Get the ConferenceToGo directory, or create if required.
                // [** 2nd callback, using anon method]
                rootDir.getDirectory("ConferenceToGo", { create: true, exclusive: false },
                    function (confDir) {
                        // Set values for future use 
                        // [** Definitely wrong scoping. The class level variable 
                        // can't be accessed using 'this'. What to do? **]
                        this.ConfRootDir = confDir;
                        // Now try getting the handle for the list file.
                        //  [** 3rd callback, using anon method. Irrelevant at this point.]
                        this.ConfRootDir.getFile(fileName, { create: false },
                            successCallback, // Success callback [getFile]
                            function (error) {
                                logError("Unable to retrieve file: ", true, true, error);
                            }); // Failure callback [getFile]
                    }, // Success callback [getDirectory]
                    function (error) { logError("Unable to create new directory: ", true, true, error); }); // Failure callback [getDirectory]
            }, // Success callback [requestFileSystem]
            function (error) { logError("Problem reading file system: ", true, true, error); }
        );
    }
}

我知道上述代码段中的作用域(通过使用“this”)都是错误的,但不知道如何正确处理。我已经看到了一些关于绑定到上下文的答案(如this one),但我使用的是匿名方法,所以这让它变得更难了。注意:虽然我这里只展示了 FileUtils 原型中的一种方法,但还有更多。

知道的人可能会认出我正在使用 cordova (PhoneGap) 库中的方法用于 HTML5 和 JS 中的跨平台移动开发,但这在这里并不重要。

【问题讨论】:

  • 您可以在嵌套函数中使用thisObj 而不是this
  • @Esailija:谢谢 .. 正在尝试,但没有完全实现。

标签: javascript closures anonymous-methods


【解决方案1】:
… function() { function() { function() { …
                    // Set values for future use 
                    // [** Definitely wrong scoping. The class level variable 
                    // can't be accessed using 'this'. What to do? **]
                    this.ConfRootDir = confDir;

您已经准备好答案:thisObj.ConfRootDirthisObj 变量在嵌套函数的范围内可用,并且仍然指向外部 getFileEntry 函数的 this keyword,即指向 FileUtils 实例。

【讨论】:

  • 哎呀..谢谢。我输入了那一行 (var thisObj = this;) 以为我会测试它,但在实际做之前就提出了这个问题。感觉很好,我很接近:-)
猜你喜欢
  • 2013-04-08
  • 1970-01-01
  • 2017-07-08
  • 2014-06-19
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多