为什么在Javascript中.length后没有()? [英] Why no () after .length in Javascript?

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

问题描述

我是一名业余程序员,我学习过多种语言,几乎总是发现'length'是一种方法/功能.据我所知,我已经受过训练,任何方法调用之后都必须带括号,即使没有参数也是如此.

I'm a hobby programmer, I've studied several languages and almost always find that 'length' is a method/function. I've been trained, from what I can tell, that any method call must be called with a parenthesis after, even with no arguments.

在Javascript中不是....为什么?

Not so in Javascript.... Why?

C#.length()ROR .lenth()

C# .length() ROR .lenth()

等...

推荐答案

在Javascript中,它是属性,而不是函数.

In Javascript it's a property, not a function.

在像C这样的语言中,长度函数必须在整个字符串中循环才能找到结尾,因此将其作为一个函数是合乎逻辑的(因为它可以完成一些工作).在长度被保留为单独值的语言中,将其改为属性是合乎逻辑的(因为它只读取一个已经存在的值).

In languages like C where the length function has to loop through the entire string to find the end, it's logical to have it as a function (as it does some work). In languages where the length is kept as a separate value, it's logical to have it as a property instead (as it only reads an already existing value).

这篇关于为什么在Javascript中.length后没有()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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