[英]How to use function caller variable in the function like parameters?
在C#中我可以創建一個像public static string ToUpper(this string str){ str.ToUpper(); }
public static string ToUpper(this string str){ str.ToUpper(); }
我想覆蓋字符串替換功能,例如:
var str = "test";
str.myReplace("var1", "var2");
如何在myReplace
獲取str
變量,如orivinal replace
函數?
以下是在javascript中擴展String
類型的方法
String.prototype.myReplace = function (var1, var2) {
// Here "this" will refer to the current value the method was invoked on
return this.replace(var1, var2);
};
然后你可以調用這樣的方法:
var str = "test";
var result = str.myReplace("var1", "var2");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.