Disons qu'un hany any object , qui renvoie une chaîne par exemple. Et j'ai le suivant :
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();
Je l'ai déjà fait en C#, mais je ne connais même pas le nom à rechercher
String.prototype.alterString=function(){
return `${this} was altered!`;
}
var obj = {
returnString : function(){ return 'Hi'; }
};
let new_string = obj.returnString().alterString();
Ou bien vous pouvez ajouter alterString() directement dans returnString() comme ci-dessous.
var obj = {
returnString : function(){
this.text = "Hey";
}
};
obj.returnString.prototype.alterString = function(){
return `${this.text} was altered!`;
}
let new_string = new obj.returnString().alterString();
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots