javascript - 我能以某种方式创建一个看起来像 .toUpperCase() 的函数吗?
问题描述
而不是使用
function functionName(arg){
arg = 1;
}
name(arg);
我想使用类似 .toUppercase() 函数
function functionName(){
this = 1;
}
arg.name();
这可能吗?
解决方案
您可以在 JavaScript 中扩展字符串对象的原型
String.prototype.functionName = function () {
return this; //do whatever you want to do it with the string
};
现在你可以这样打电话了mystring.functionName()
推荐阅读
- sorting - 在 Excel-Dna 中对 Excel“变体”进行排序的 F# 函数
- ruby-on-rails - 如何在 Rails 中仅使用 10 分钟的“编辑帖子”方法?
- sql - 如何获得每个相似列值的平均值?
- woocommerce - 如何从钩子 woocommerce_subscription_totals_table 中删除 get_subscription_totals_template 函数
- android - 如何在多个线程中运行 webview
- jboss - 错误从 jboss6 更新到 wildfly15 时无法处理部署的阶段 POST_MODULE
- python - 为什么我的 If/Else 循环会脱离我的 For 循环?
- javascript - Styled-Components 的 .attrs() 函数的用例是什么?
- javascript - 使用 javascript 或 jquery 复制在特定文件夹中的输入标记中选择的文件夹
- javascript - 打字稿/角度中的动态谷歌可视化