Javascript Prototype pub sub方法超出范围

时间:2013-03-05 18:37:40

标签: javascript prototype publish-subscribe

在我的构造函数中,我订阅了另一个类函数。如何在订阅中调用类方法show。

var MemPortfolio = MemPortfolio || {};
MemPortfolio.Portfolio = function (contentid, thumbclass, featuredid) {
    this.thumbClass = thumbclass;
    this.featuredId = "#" + featuredid;
    var that = this;
    //subscribe to menutile click
    $.subscribe("/menutile/click/portfolio", function (tilename) {
        that.Show();
    });
};

MemPortfolio.Portfolio.prototype.Show = function () {
    alert("show");

}

在我的文件准备中: /// ///

$(document).ready(function () {
    //check exists?
    var tilemenu = new MemTileMenu.Menu("menu", "menuitem", "contentarea");
    var portfolio = new MemPortfolio.Portfolio("contentarea", "thumb", "");
});

我得到了这个 -

/// <reference path="jquery-1.7.1.js" />
/// <reference path="jquery.ba-tinypubsub.js" />
var MemPortfolio = MemPortfolio || {};
MemPortfolio.Portfolio = function (contentid, thumbclass, featuredid) {
    this.thumbClass = thumbclass;
    this.featuredId = "#" + featuredid;    
};

MemPortfolio.Portfolio.prototype = {

    show: function () {
        alert("show");
    },
    subscribe: function () {
        var that = this;
        $.subscribe("/menutile/click/portfolio", function (tilename) {
            that.show();
        });
    }

Doc.Ready here:

/// <reference path="tilemenu.js" />
/// <reference path="portfolio.js" />
$(document).ready(function () {
    //check exists?
    var tilemenu = new MemTileMenu.Menu("menu", "menuitem", "contentarea");
    var portfolio = new MemPortfolio.Portfolio("contentarea", "thumb", "");
    portfolio.subscribe();
});
    }

感谢。 NerdPerson

2 个答案:

答案 0 :(得分:0)

尝试按MemPortfolio.Portfolio.prototype

替换MemPortfolio.prototype.Portfolio

答案 1 :(得分:0)

这很有效。

  /// <reference path="jquery-1.7.1.js" />
    /// <reference path="jquery.ba-tinypubsub.js" />
    var MemPortfolio = MemPortfolio || {};
    MemPortfolio.Portfolio = function (contentid, thumbclass, featuredid) {
        this.thumbClass = thumbclass;
        this.featuredId = "#" + featuredid;    
    };

    MemPortfolio.Portfolio.prototype = {

        show: function () {
            alert("show");
        },
        subscribe: function () {
            var that = this;
            $.subscribe("/menutile/click/portfolio", function (tilename) {
                that.show();
            });
        }
    Doc.Ready here:

    /// <reference path="tilemenu.js" />
    /// <reference path="portfolio.js" />
    $(document).ready(function () {
        //check exists?
        var tilemenu = new MemTileMenu.Menu("menu", "menuitem", "contentarea");
        var portfolio = new MemPortfolio.Portfolio("contentarea", "thumb", "");
        portfolio.subscribe();
    });
        }