为什么 .length 不是一个需要 () 的方法 [英] why isnt .length a method that requires ()

查看:68
本文介绍了为什么 .length 不是一个需要 () 的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 JavaScript 中,为什么关键字长度不需要 ()?似乎所有其他函数,如 splice()、shift() 都需要括号.

In JavaScript why doesn't the keyword length require ()? It seems all the other functions such as splice(), shift() require parenthesis.

推荐答案

Length 不是一种方法.它是一种财产.请参阅MDN

Length is not a method. It is a property. See MDN

调用属性时不需要括号.属性是对象自己知道"的东西.javascript 中的数组或字符串知道它的长度.方法是需要计算的东西.

When calling properties you do not need parentheses. A property is something that an object "knows" about it self. An array or a string in javascript knows its length. A method is something that needs to be calculated.

如果 javascript 真的需要在每次需要长度时计算字母,长度可能是 javascript 字符串上的一种方法.

Length could have been a method on javascript strings if javascript had really needed to count the letters each time you want the length.

当从属性中获取值时,您可以假设获取其值比从方法调用中获取值更快.(虽然情况并非总是如此)

When get a value from a property you can make an assumption that it is quicker to get its value than it would be getting a value from a method call. (Although this is not always the case)

这篇关于为什么 .length 不是一个需要 () 的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆