避免重复javascript原型定义-上下文问题

避免重复javascript原型定义-上下文问题,javascript,this,es6-modules,Javascript,This,Es6 Modules,我有一些javascript代码,看起来是这样的-非常重复,正如您所看到的,下面是一个非常明确的模式: var AttachmentBuilder = function(){ this.attachment = {}; } AttachmentBuilder.prototype.text = function(value){ this.attachment.text = value; return this; } AttachmentBuilder.prototype.f

我有一些javascript代码,看起来是这样的-非常重复,正如您所看到的,下面是一个非常明确的模式:

var AttachmentBuilder = function(){
    this.attachment = {};
}
AttachmentBuilder.prototype.text = function(value){
    this.attachment.text = value;
    return this;
}
AttachmentBuilder.prototype.fallback = function(value){
    this.attachment.fallback = value;
    return this;
}
 AttachmentBuilder.prototype.color = function(value){
    this.attachment.color = value;
    return this;
}
我的想法是重构如下:

var AttachmentBuilder = function(){
    this.attachment = {};
}
passThrough(AttachmentBuilder.prototype,"attachment","text");
passThrough(AttachmentBuilder.prototype,"attachment","fallback");
passThrough(AttachmentBuilder.prototype,"attachment","color");

function passThrough(obj, apply, name){
    obj[name] = function(param){
        this[apply][name] = param;
    }
    return this;
 }
但是
这个
的上下文是不正确的,并且它的行为与长手稿不同

下面是演示工作版本和非工作版本的工作示例

var AttachmentBuilder\u Original=function(){
this.attachment={};
}
AttachmentBuilder_Original.prototype.text=函数(值){
this.attachment.text=值;
归还这个;
}
AttachmentBuilder_Original.prototype.fallback=函数(值){
this.attachment.fallback=值;
归还这个;
}
AttachmentBuilder_Original.prototype.color=函数(值){
this.attachment.color=值;
归还这个;
}
var original=new AttachmentBuilder_original();
原始。文本(“文本”)。颜色(“红色”)。回退(“回退”);
控制台日志(“原件”,原件。附件);
/* ------------------------------------- */
var AttachmentBuilder_New=函数(){
this.attachment={};
}
传递(AttachmentBuilder_New.prototype,“附件”,“文本”);
直通(AttachmentBuilder_New.prototype,“附件”,“回退”);
passThrough(AttachmentBuilder_New.prototype,“附件”,“颜色”);
函数传递(对象、应用、名称){
obj[name]=函数(参数){
此[应用][名称]=参数;
}
归还这个;
}
var adjusted=new AttachmentBuilder_new();
调整。文本(“文本”)。颜色(“红色”)。回退(“回退”);

控制台日志(“已调整”,已调整。附件)您的高阶函数看起来不错。可能是把return语句放错地方的简单错误

function passThrough(obj, apply, name){
    obj[name] = function(param){
        this[apply][name] = param;
        return this;
    } //^^^^^^^^^^^^
}

如果您进行此修改,您的解决方案应该有效

var AttachmentBuilder = function(){
        this.attachment = {};
    }
    passThrough(AttachmentBuilder.prototype,"attachment","text");
    passThrough(AttachmentBuilder.prototype,"attachment","fallback");
    passThrough(AttachmentBuilder.prototype,"attachment","color");

    function passThrough(obj, apply, name){
        obj[name] = function(param){
            this[apply][name] = param;
            return this;//<---move return this here
        }
        //return this;
     }
var AttachmentBuilder=function(){
this.attachment={};
}
passThrough(AttachmentBuilder.prototype,“附件”,“文本”);
passThrough(AttachmentBuilder.prototype,“附件”、“回退”);
passThrough(AttachmentBuilder.prototype,“附件”,“颜色”);
函数传递(对象、应用、名称){
obj[name]=函数(参数){
此[应用][名称]=参数;

返回此;//您的意思是
返回此
内部函数?现在是在es6中编写代码并使用transpiler+bundler来支持es5的时候了。可以让您摆脱这些障碍并专注于业务逻辑。只是一个建议。这是一个nodeJS项目,所以远离es6!感谢您称我为n00b。我只写了25年的javascript!但是d-我把我的
返回到了错误的地方。嗯,这可能是糟糕的语言使用。语言不是那么熟练,这种情况经常发生。我的意思是简单的错误,如果那是糟糕的话,很抱歉。没关系。如果你有一个更像ES6的解决方案,我最感兴趣。我仍然停留在老式的JS思维中,我认为ES6没有解决方案任何与链接相关的特别内容,所以我最好收回我对此的免费意见。如果我发现任何与此模式相关的内容,我将在这里更新。