javascript子类和方法重写

时间:2014-02-17 14:50:20

标签: javascript class oop design-patterns factory-pattern

我通过这种模式编写了以下javascript类(工厂): http://javascript.info/tutorial/factory-constructor-pattern

var lnq = function(domain) {

    var ...;

    var testLink = function(href) {
      ....
    };

    var addAllLinks = function(links) {
        ...
    };

    var normalize = function(links) {
        ...
    };

    var searchAllLinks = function(address, callback) {
        ....
    };

    var buildAddress = function(query) {
        ....
    };

    var searchCurrentPageLinks = function() {
        ....
    };

    var toNextPage = function() {
        ....
    };

    var findNextPageLink = function() {
        ....
    };

    // public interface
    return {

        // search all links by query
        searchLinksByQuery: function(query, callback) {
            .....
        }
    }
};

它也有效。如你所见,它有很少的私人变量&方法和公共区域(SearchLinksByQuery)。我需要创建子类,并覆盖3个方法:searchCurrentPageLinks,toNextPage,findNextPageLink。我该如何实现呢?

1 个答案:

答案 0 :(得分:0)

http://javascript.info/tutorial/factory-constructor-pattern#inheritance应用于您的问题:

function LnqSub(domain) {
     var l = lnq(domain);
     l.searchCurrentPageLinks = …;
     l.toNextPage = …;
     l.findNextPageLink = …; // overwrite them
     return l;
}