假设任意一个对象,例如返回字符串。我有以下内容:
function alterString(str){
return `${str} was altered!`
}
//I want call my method as if it were be inside the return type
let new_string = object.returnString().alterString();
我已经在c#中这样做了,但我甚至不知道要研究
的名称String.prototype.alterString=function(){
return `${this} was altered!`;
}
var obj = {
returnString : function(){ return 'Hi'; }
};
let new_string = obj.returnString().alterString();
或者你可以直接将alterString()添加到returnString()中,如下所示。
var obj = {
returnString : function(){
this.text = "Hey";
}
};
obj.returnString.prototype.alterString = function(){
return `${this.text} was altered!`;
}
let new_string = new obj.returnString().alterString();