如何在“。”之后调用函数,javascript

baf*_*203 2 javascript

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

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

console.log("check_length".myfunc())
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?预先感谢您的答复!

Nin*_*olz 5

唯一的方法是String使用经典的访问功能来变异的原型this

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

console.log("check_length".myfunc());
Run Code Online (Sandbox Code Playgroud)

  • 正确。另外,请**永远不要这样做。** (3认同)