首页 > 解决方案 > 我能以某种方式创建一个看起来像 .toUpperCase() 的函数吗?

问题描述

而不是使用

function functionName(arg){
arg = 1;
}
name(arg);

我想使用类似 .toUppercase() 函数

function functionName(){
this = 1;
}
arg.name();

这可能吗?

标签: javascript

解决方案


您可以在 JavaScript 中扩展字符串对象的原型

String.prototype.functionName = function () {
    return this; //do whatever you want to do it with the string
};

现在你可以这样打电话了mystring.functionName()


推荐阅读