我应该如何处理javascript类中匿名函数的属性?

我应该如何处理javascript类中匿名函数的属性?,javascript,Javascript,我不熟悉面向对象的javascript,所以可能我的术语弄错了 我有一个javascript类,看起来像这样 var parsesearch = { init: function(selector){ this.selector = $(selector); this.count = $(selector).length; }, loopresluts: function(selector){ this.delay = 35

我不熟悉面向对象的javascript,所以可能我的术语弄错了

我有一个javascript类,看起来像这样

var parsesearch = {
    init: function(selector){
        this.selector = $(selector);
        this.count = $(selector).length;
    },
    loopresluts: function(selector){
        this.delay = 350
        this.selector.each(function(indexInArray) {
            parsesearch.announcementID = parsesearch.selector.find('[headers=h-diarienummer] a').text();
            //opening the pages with a delay
            setTimeout( function () {
                console.log(parsesearch.announcementID)
            //adding delay for every itteration.
            }, indexInArray * parsesearch.delay);
        });
    }
}

我有一个匿名函数。在该函数中,我不能使用“this”来引用属性,而是使用parsesearch。“propertyname”我假设如果我引用了多个版本的parsesearch,这将导致麻烦。有更好的方法吗?如果我只使用这个类的一个实例,这会被认为是不好的做法吗?在这种情况下,为什么

您可以将其修改为以下内容:

 var parsesearch = (function () {
    var init = function (selector) {
        this.selector = $(selector);
        this.count = $(selector).length;
    }
    var loopresluts = function (selector) {
        var self = this; // self now refers to that function only
        this.delay = 350
        this.selector.each(function (indexInArray) {
            parsesearch.announcementID = parsesearch.selector.find('[headers=h-diarienummer] a').text();
            //opening the pages with a delay
            setTimeout(function () {
                console.log(parsesearch.announcementID)
                //adding delay for every itteration.
            }, indexInArray * parsesearch.delay);
        });
    }
    return {
        init: init,
        loopresluts: loopresluts
    }
});

// create a single instance of your object
var parseS = parsesearch();

您只需使用一个变量来保存当前实例(this),它将在匿名函数使用的范围内

loopresluts: function(selector){
    this.delay = 350;
    var scoped = this;
    this.selector.each(function(indexInArray) {
        scoped.announcementID = parsesearch.selector.find('[headers=h-diarienummer] a').text();
        //opening the pages with a delay
        setTimeout( function () {
            console.log(scoped.announcementID)
        //adding delay for every itteration.
        }, indexInArray * scoped.delay);
    });
}

最简单的方法是使用闭包

var parsesearch = {
    init: function(selector){
        this.selector = $(selector);
        this.count = $(selector).length;
    },
    loopresluts: function(selector){
        var self = this;
        this.delay = 350
        this.selector.each(function(indexInArray) {
            self.announcementID = self.selector.find('[headers=h-diarienummer] a').text();
            //opening the pages with a delay
            setTimeout( function () {
                console.log(self.announcementID)
            //adding delay for every itteration.
            }, indexInArray * self.delay);
        });
    }
}
或者,因为您已经在使用
jQuery
,所以可以使用
jQuery.proxy

var parsesearch = {
    init: function(selector){
        this.selector = $(selector);
        this.count = $(selector).length;
    },
    loopresluts: function(selector){
        this.delay = 350
        this.selector.each($.proxy(function(indexInArray) {
            this.announcementID = this.selector.find('[headers=h-diarienummer] a').text();
            //opening the pages with a delay
            setTimeout($.proxy(function () {
                console.log(this.announcementID)
            //adding delay for every itteration.
            }, this), indexInArray * this.delay);
        }, this));
    }
}
绑定方法 您可以将函数绑定到对象,例如,
this

var parsesearch = {
    init: function(selector){
        this.selector = $(selector);
        this.count = $(selector).length;
    },
    loopresluts: function(selector){
        this.delay = 350
        this.selector.each((function(indexInArray) {
            this.announcementID = this.selector.find('[headers=h-diarienummer] a').text();
            //opening the pages with a delay
            setTimeout( function () {
                console.log(this.announcementID)
            //adding delay for every itteration.
            }, indexInArray * this.delay);
        }).bind(this));
    }
}
范围法 通过声明引用此的局部变量,可以在嵌套函数中使用该变量

var parsesearch = {
    ...
    loopresluts: function(selector){
        var p = this;
        this.delay = 350
        this.selector.each(function(indexInArray) {
            p.announcementID = p.selector.find('[headers=h-diarienummer] a').text();
            //opening the pages with a delay
            setTimeout( function () {
                console.log(p.announcementID)
            //adding delay for every itteration.
            }, indexInArray * p.delay);
        });
    }
}

哇,这么多问题。让我们一步一步来。当你开始的时候

var parsesearch = { ...
您使用了singleton,因此基本上无法再执行更多的parsesearch实例。您只有一个实例及其函数,仅此而已

如果我只使用这个类的一个实例,这会是一种不好的做法吗?在这种情况下,为什么

这取决于它的目的是什么。如果您将扩展此“小部件”,并添加选项和其他内容+,您将希望在一个站点上使用更多小部件,那么您可能需要更多实例。在此之前,只有一个实例是可以的,您可以保持原样

关于
这个问题,我基本上同意zaquest和*Thanassis_K*的答案。您可以使用
调用
应用
本机函数或jQuery
代理
函数来更改所需函数的
。此外,
var self=这是非常常见的做法。然后,
self
是名称空间的变量。无论如何,出于某些原因,这里所有的答案都是避免原型,这里应该使用原型,因为如果你将函数声明为简单的对象属性,那么它们会在每个新实例中重新声明,这是浪费

我的解决方案适用于更多实例

var ParseSearch = function (selector) {
    this.selector = $(selector);
    this.count = $(selector).length;
    this.delay = 350;
}

// you dont need init here... 

ParseSearch.prototype.loopresluts = function(){
    this.selector.each($.proxy(function(indexInArray) {
        this.announcementID = this.selector.find('[headers=h-diarienummer] a')
                                           .text();
        //opening the pages with a delay
        setTimeout($.proxy(function () {
            console.log(this.announcementID)
        //adding delay for every itteration.
        }, this), indexInArray * this.delay);
    }, this));
}

var firstParser = new ParseSearch('div');
firstParser.loopresluts();

这不是一个类,它最像一个单例实例,这是一种不用在OOP语言中使用OOP就可以摆脱的方式。实际上:如果使用绑定,就不要再使用
self
。我不知道为什么需要很多这样的实例,这些应该合并到单个函数调用中,因为它就是这样使用的,除了调用一个方法并扔掉它之外,你永远不会对对象做任何事情,这是一个需要简化单个函数的危险信号。由于硬编码字符串选择器,该类的上下文也极不可用。但是+1作为原型,答案本身就是如何获得Javascript对象模型的全部好处。是的,我同意你刚才所说的一切。我以为他会用更多的行为来扩展它,所以我就这样做了。我还可以写得更好,但对于一个“新手”来说,它可能显得太复杂了。所以让他再发一个问题;)PS:thx用于编辑