0

我刚刚开始使用原型,我正在尝试在我正在开发的网站上测试不同的东西。但是我偶然发现了一个错误,我不确定为什么,因为我之前使用过该属性并且它有效。

我不是经验丰富的 JavaScript 开发人员,我还在学习,但我得到了:

var defaults = {
    local_storage_key   : "Cluster",
    plugin_navigation   : ".navigation",
    plugin_wrapper      : "content-wrapper",
    iqns_class          : ".iqn"
}

var Clusters = function(environment, options){

    this.options = $.extend({}, defaults, options);

    this.environment = environment;

    this.viewport = $(window);

    this.viewport_width = this.viewport.width();
    this.viewport_height = this.viewport.height();

    this.data_key = this.options.local_storage_key;

    this.iqns_class = this.viewport.find(this.options.iqns_class);

    this.iqn = this.iqns_class.parent();

    this.shop_initiated = false;

    this.plugin_navigation = this.options.plugin_navigation;

    this.plugin_wrapper = this.options.plugin_wrapper;

    this.initiate_plugin(this.plugin_navigation, {
        containerID : this.plugin_wrapper,
        first       : false,
        previous    : false,
        next        : false,
        last        : false,
        startPage   : this.get_local_storage_data(),
        perPage     : 6,
        midRange    : 15,
        startRange  : 1,
        endRange    : 1,
        keyBrowse   : false,
        scrollBrowse: false,
        pause       : 0,
        clickStop   : true,
        delay       : 50,
        direction   : "auto",
        animation   : "fadeInUp",
        links       : "title",
        fallback    : 1000,
        minHeight   : true,
        callback    : function(pages) {
            this.set_local_storage_data(pages.current);
        }
    });

    this.initiate_auxiliars();

    this.set_environment();

};

Clusters.prototype.set_local_storage_data = function(data_val) {
    return localStorage.setItem(this.data_key, data_val);
};

Clusters.prototype.get_local_storage_data = function() {
    return +(localStorage.getItem(this.data_key) || 1);
};

Clusters.prototype.shop_iqns_selected_class = function() {
    var self = this;
    if (this.viewport_width < 980) {
        $(this.iqns_class).each(function(index, element) {
            var element = $(element);
            $(self.iqn).on('click', function() {
                if (element.hasClass('selected')) {
                    element.removeClass('selected');
                } else {
                    element.addClass('selected');
                }
            });
        });
    }
}

Clusters.prototype.initiate_plugin = function(plugin_navigation, plugin_options) {
    return $(plugin_navigation).jPages(plugin_options);
}

Clusters.prototype.initiate_auxiliars = function() {
    return this.shop_iqns_selected_class();
}

Clusters.prototype.set_environment = function() {
    if(this.environment == "Development") {
        less.env = "development";
        less.watch();
    }
}

var cluster = new Clusters("Development");

我确定我在原型设计方面做错或误解了一些事情,因为否则我不会得到任何错误。我只是就我做错了什么,我应该做什么或不应该做什么征求一些意见或方向。

这是我得到的错误:

Uncaught TypeError: Object #<Object> has no method 'set_local_storage_data' on line 53
4

1 回答 1

1

当您使用回调函数时,this关键字将不再引用集群对象。回调将在“没有方法'set_local_storage_data'”的其他上下文中执行。

为避免这种情况,您可以使用bind()集群对象的函数,或者使用构造函数范围内的局部变量来引用集群对象:

var that = this;
this.initiate_plugin(
    ...,
    function(pages) {
        that.set_local_storage_data(pages.current);
    }
);
于 2012-05-14T00:30:53.780 回答