簡體   English   中英

如何在函數中使用函數調用變量?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM