0

我有这样的代码:

var SEVERINU = SEVERINU || {};

SEVERINU.AutoGallery = {
    galleryImagesNames: new Array(),

    findImages: function () {
        var dirToResearch = this.galleryName;

        $.post(
            "_php/get-files.php", {
            dir: dirToResearch,
            ext: this.fileExtension
        }, function (data) {
            var pliki = $.parseJSON(data);
            console.log(data);
            for (var i in pliki) {
                this.galleryImagesNames.push(pliki[i]); // problem !
            }
        });

    },
}

这条线:this.galleryImagesNames.push(pliki[i]);给我带来了问题。

它说他找不到 var galleryImagesNames 等。

如果我要“深”,如何调用函数,var?

4

4 回答 4

3

只需将当前保存this到其他变量,这样它就不会在函数内部被覆盖。

var SEVERINU = SEVERINU || {};

SEVERINU.AutoGallery = {
  galleryImagesNames : new Array(),

  findImages : function(){
        var self = this; // keep current value of this in variable named self
        var dirToResearch = this.galleryName;

        $.post(
            "_php/get-files.php",
            {
                dir: dirToResearch,
                ext: this.fileExtension
            },
            function(data){
                var pliki = $.parseJSON(data);
                console.log(data);
                for(var i in pliki){
                    // call the method using self variable instead of this that got overwritten
                    self.galleryImagesNames.push(pliki[i]); 
                }
            }
        );

    },

}
于 2013-05-16T17:43:02.000 回答
1

实际上这是因为您$.post的范围与对象的其余部分不同。尝试这个:

findImages : function(){
    var dirToResearch = this.galleryName;
    var gImageNames = this.galleryImagesNames;
    $.post(
        "_php/get-files.php",
        {
            dir: dirToResearch,
            ext: this.fileExtension
        },
        function(data){
            var pliki = $.parseJSON(data);
            console.log(data);
            for(var i in pliki){
                gImageNames.push(pliki[i]); // we have access to this variable
            }
        }
    );

},
于 2013-05-16T17:43:14.397 回答
1

由于您在匿名函数中,因此您将不得不this再次引用该关键字。或者像这样直接使用它:

SEVERINU.AutoGallery.galleryImagesNames.push(pliki[i]);

通常,当您SEVERINU.AutoGallery多次需要引用时,最好将其存储在变量中,例如:

var that = SEVERINU.AutoGallery; // or "self" or something you prefer

for (var i in pliki) {
    that.galleryImagesNames.push(pliki[i]);
}

这是因为 javascript 在访问对象命名空间时往往很慢。你走得越深,它就越慢。您进行的引用越多,它就会变得越慢,因为必须首先解析函数范围才能访问下一个/上一个范围。

于 2013-05-16T17:44:25.647 回答
1

您还可以使用$.proxy来传递上下文。

var SEVERINU = SEVERINU || {};

SEVERINU.AutoGallery = {
 galleryImagesNames : new Array(),

 findImages : function(){
    var dirToResearch = this.galleryName;

    $.post(
        "_php/get-files.php",
        {
            dir: dirToResearch,
            ext: this.fileExtension
        },
        $.proxy(function(data){ //Set up for the context to be passes for this function.
            var pliki = $.parseJSON(data);
            console.log(data);
            for(var i in pliki){
                this.galleryImagesNames.push(pliki[i]); // problem solved. now this doesn't refer to ajax context
            }
        },this) <-- Pass the context here
    );

},
}

$.proxy 接受一个函数并返回一个始终具有特定上下文的新函数。

于 2013-05-16T17:45:10.790 回答