Mike Varela
Mike Varela

Reputation: 527

Why no () after .length in Javascript?

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.

Not so in Javascript.... Why?

C# .length() ROR .lenth()

etc...

Upvotes: 12

Views: 6767

Answers (7)

Anurag
Anurag

Reputation: 141879

There are many languages where properties can be backed by method calls including JavaScript. It is up to the implementors to decide how it will be implemented.

You can have your own properties backed by functions. I've already answered a similar question before, so won't repeat it here.

Ruby also does not require parentheses after a method call. This is valid Ruby code

[].size          // size is an Array method
object.x = 42;   // x= is a method (setter)

Python also also has the concept of function backed properties. Here's an example in Python:

class Foo(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        print "getter called"
        return self._x

    @x.setter
    def x(self, value):
        print "setter called"
        self._x = value

f = Foo()
f.x = "20"
setter called
f.x
getter called

Parentheses is just syntax and has traditionally been used for function/method calls, but it's not a universal standard by any means.

Upvotes: 0

gilly3
gilly3

Reputation: 91497

In JavaScript, .length is a property and behaves as one:

var a = [1, 2, 3, 4, 5];
console.log(a.length); // returns 5
a.length = 3;
console.log(a.join(",")); // returns "1,2,3"

http://jsfiddle.net/GD89N/

Notice you can not only read, but also write the value of the property.


By the way, In C#, usually .Length or .Count are properties, not methods:
String.Length
Array.Length
List<T>.Count

Linq exposes a Count() method to IEnumerable<T>, but that's a relatively new addition.

Upvotes: 0

Guffa
Guffa

Reputation: 700342

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

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).

Upvotes: 14

Bojangles
Bojangles

Reputation: 101483

In JavaScript, .length is a property of the array, not a function of the prototype. Imagine your array is represented like this:

array {
    data: [ ... ],
    length: 4    // 4 elements
}

This is obviously nonsense, but the point is that .length is a property of the array object, not a function of it, so it doesn't need trailing brackets.

Upvotes: 3

Silas Hansen
Silas Hansen

Reputation: 1739

Because its a known value on the object (a property) and not a function that needs to be evaluated.

Upvotes: 1

JaredPar
JaredPar

Reputation: 754735

The name length is a property not a function. A property can be accessed by name while a function must be invoked with () and possibly arguments to produce a value.

For example

var x = {};
x.name = "example";  // Property 
console.log(x.name);  // Prints "example"
x.getName = function() { return "example"; }  // Function
console.log(x.getName);  // Doesn't print what you'd want here
console.log(x.getName());  // Prints "example"

Upvotes: 2

shadyabhi
shadyabhi

Reputation: 17234

It's because it's a property, not a function.

You can find the relevant documentation here at MDN.

This property returns the number of code units in the string.

Upvotes: 4

Related Questions