在你的问题中的代码是不能反映一个适当的实施,但回答你的直接问题,是的,这...
UI.folders().getSelectedFolder()
...将是一个方法链的例子。
装饰模式是不同的。如果你有一组方法,每个人应该总是先调用一些常用的功能,你可以创建一个装饰,将返回首先调用常见的一种,那么实际的一个功能...
function foo() {
console.log('I\'m foo, and I\'m first, and I was given these args:', arguments);
}
function decorateWithFoo(decorated) {
return function() {
foo.apply(this, arguments);
decorated.apply(this, arguments);
};
}
所以,你可以使用decorateWithFoo
创建总是调用foo
第一功能...
// create and decorate bar()
var bar = function(a,b) {
console.log('I\'m bar, and I was called after "foo", and was given args:', a, b);
};
bar = decorateWithFoo(bar);
bar(123, 456); // this will first call `foo()`, then (the original) `bar()`.
// create and decorate baz()
var baz = function(a,b) {
console.log('I\'m baz, and I was called after "foo", and was given args:', a, b);
};
baz = decorateWithFoo(baz);
baz(123, 456); // this will first call `foo()`, then (the original) `baz()`.
一些语言已经建立了用于创建装饰器的语法。 JavaScript目前没有。
如果你发现自己使用的装饰以不同的方式,你可以创建,设置了最初的装饰功能的其他功能...
function generateDecorator(decorator) {
return function (decorated) {
return function() {
decorator.apply(this, arguments);
decorated.apply(this, arguments);
};
};
}
所以我们原来decoreateWithFoo
可能已经建立了这样的...
function foo() {
console.log('I\'m foo, and I\'m first, and I was given these args:', arguments);
}
var decorateWithFoo = generateDecorator(foo);
你没有正确地进行继承。 '... folders.prototype.getSelectedFolder'没有任何好处,因为'UI.prototype.folders'只返回一个普通数组。为'folders.prototype'产生效果,你需要调用'folders'作为构造函数,并让它返回被构造的对象而不是Array。 – 2012-04-15 17:25:42
...但是,正确实施,'UI.folders()。getSelectedFolder()'是方法链接的一个例子。 – 2012-04-15 17:26:43
+1我不确定为什么投票反对,这不仅仅是一个很好的描述性问题,而且还包括了你想要做的事情。 – 2012-04-15 17:32:25