lag*_*lex 5 javascript method-chaining
我在一个对象中有两个函数
var obj = {};
obj.fn1 = function(){ console.log('obj.fn1'); return this; };
obj.fn2 = function(){ console.log('obj.fn2'); return this; };
Run Code Online (Sandbox Code Playgroud)
我希望能够做到
obj.fn1.fn2()
obj.fn2.fn1()
Run Code Online (Sandbox Code Playgroud)
这该怎么做?
编辑:fn1和fn2可能是功能或属性访问器,我不只要关心他们做的东西.
我在许多图书馆看过它,比如粉笔 color.red.bold('string').
我认为这只是一些嵌套的对象。看一下这段代码:
function Color(c) { this.color = c; }
Color.prototype.bold = function(text) { return '<strong style="color: ' + this.color + '">' + text + '</strong>'; }
var colors = {
red: new Color('red'),
blue: new Color('blue')
}
document.write(
colors.red.bold('text'));
document.write(
colors.blue.bold('text'));Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |