在javascript中创建方法链是否有众所周知的模式

Are there well known patterns to use for creating method chaining in javascript?

本文关键字:众所周知 模式 是否 javascript 创建 方法      更新时间:2023-09-26

在javascript中实现方法链的最佳方式是什么?

function CONSTRUCT(){
    this.func1=function(insert_ob){insert_ob.appendTo=this;/*other code*/};
    this.func2=function(...){};
    this.func3=function(...){};
}
function My_Object_Creator(){this.appendTo=null;}
object1=new CONSTRUCT();
my_object=new my_Object_Creator();
object1.func1(my_object).func2().func3();

我知道我可以在构造函数中调整这些函数来实现它,但我想知道是否有一些众所周知的方法或模式应该遵循,而在javascript中创建方法链接的可能性?

方法链最基本的方法是返回this(或对象的引用)。