javascript 如何在javascript中制作“点函数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19872917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:06:17  来源:igfitidea点击:

How to make a "dot function" in javascript

javascriptfunction

提问by Oliver Ni

I'm trying to define a "dot function" where there are no parameters but has a .and a string or number before it like these:

我试图定义一个“点函数”,其中没有参数,但.在它之前有一个和一个字符串或数字,如下所示:

.toUpperCase()
.toLowerCase()
.indexOf()
.charAt()
.substring()

.toUpperCase()
.toLowerCase()
.indexOf()
.charAt()
.substring()

You do 2..toString, not toString(2).

你有2..toString,没有toString(2)

How do you define one of them?

你如何定义其中之一?

And why when I type 4..toString()it returns function toString() {[native code]}?

为什么当我输入4..toString()它时返回function toString() {[native code]}

采纳答案by Paul Draper

Defining a "dot function" is easy. Here's how you can define it on a single object.

定义“点函数”很容易。以下是在单个对象上定义它的方法。

var a = {}, or a = function() {}, or a = [], etc.

a.dotFunction = function() { return 'hi'; }

console.log(a.dotFunction());

If you want to define it on all instances of a "class", use prototype.

如果要在“类”的所有实例上定义它,请使用prototype.

function someClass() {
}

someClass.prototype.dotFunction = function() { return 'hi'; };

console.log(new someClass().dotFunction());

You can even do this on built-in types (some, like Prototype.js, do this, though most recommended against it).

您甚至可以在内置类型上执行此操作(有些,例如 Prototype.js,执行此操作,但大多数人建议不要这样做)。

Number.prototype.dotFunction = function() { return 'hi'; };

console.log((0).dotFunction()); 

回答by p.s.w.g

I'd strongly recommend not trying to replace any built-in methods, however, you're free to define your own methods however you like.

我强烈建议不要尝试替换任何内置方法,但是,您可以随意定义自己的方法。

You can do this by attaching the method to the Numberor Stringtype's prototype:

您可以通过将方法附加到NumberString类型的原型来做到这一点:

Number.prototype.foo = function(n) { return this * n; };
String.prototype.bar = function(n) { return this.length * n; };

alert(4..foo(2));  // 8
alert("4".bar(2)); // 2

Further Reading

进一步阅读

回答by HMR

I'll give it a shot because nobody mentioned that you can already do this without having to define anything yourself.

我会试一试,因为没有人提到你已经可以做到这一点,而不必自己定义任何东西。

A thing to take care of is if you have a number you have to place 2 dots after it where as if you have a function that returns a number or a variable that holds one you don't:

需要注意的是,如果您有一个数字,则必须在其后放置 2 个点,就像您有一个返回数字的函数或一个包含您没有的数字的变量一样:

1..toString()
 .indexOf("1")//<=returns number 0
 //even though a number is returned we only need one dot here
 .toString();//<="0"

var num = 1234;
num.toString()//<=one dot
  .indexOf("23");//<=1

Your example would already work but since indexOfwould return a number if you give it an argument that makes sense and a number doesn't have a charAtmethod.

你的例子已经可以工作了,但是indexOf如果你给它一个有意义的参数并且一个数字没有charAt方法,它就会返回一个数字。

"hello".toUpperCase()
  .toLowerCase()
  .indexOf("h")//<=returns a number 
               //number has no charAt method
  .toString()
  .charAt(0)
  .substring(0);//<="0"