【问题标题】:How to call function after ".", javascript [duplicate]如何在“。”之后调用函数,javascript [重复]
【发布时间】:2019-07-01 15:44:21
【问题描述】:

我有这个示例函数,我想以这种格式调用它:

const myfunc = (string) => {
   return string.length
}

console.log("check_length".myfunc())

我该怎么做? 提前感谢您的回答!

【问题讨论】:

  • 所以你必须扩展字符串原型

标签: javascript


【解决方案1】:

唯一的办法是用访问this的经典函数改变String的原型。

String.prototype.myfunc = function () {
   return this.length;
};

console.log("check_length".myfunc());

【讨论】:

  • 正确。另外,请永远不要这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2013-08-17
  • 2015-06-13
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 2017-11-22
相关资源
最近更新 更多