我通过这种模式编写了以下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。我该如何实现呢?
答案 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;
}