如何在不执行的情况下使函数可链接?比如`obj.fn.fn`而不是`obj.fn().fn()`

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)

这该怎么做?

编辑:fn1fn2可能是功能或属性访问器,我不只要关心他们做的东西.

我在许多图书馆看过它,比如粉笔 color.red.bold('string').

Ada*_*sko 0

我认为这只是一些嵌套的对象。看一下这段代码:

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)