11

Suppose I define a variable like this

var today = Date();
console.log(today.getMonth()); // Throw Error

while other class like Error class call their methods without new operator.

function factorial(x) {
 if(x <= 1)
   throw Error("x must not be negative");
 return x*factorial(x-1);
}

Also wrapper objects (number, boolean, string) can call their methods without new operator. So, Is this the only class which require new operator or any object creation technique before calling their methods.

Edit: As Date() is a string type, so it should be call their methods without creating objects. Because string type behave as if they are objects. So why not?

Edit 2: I think this is the only core function which cannot be same as new Date() like other functions (Array(), String(), Error() etc). So, it's also the hidden feature of this language or ECMAScript mistake.

Community
  • 1
  • 1
Ashish Rawat
  • 3,363
  • 5
  • 29
  • 35
  • 4
    `typeof Date() === 'string'` – jantimon May 14 '13 at 13:07
  • Which is a little unfortunate. Having Date being a function giving the current date as a string is, at best, a compatibility feature that should have been cleaned as useless. – Denys Séguret May 14 '13 at 13:10
  • @dystroy i'm yet to see a language that got everything related to date handling right – soulcheck May 14 '13 at 13:12
  • @soulcheck Some very recent ones seem to have got it right ([for example go](http://golang.org/pkg/time/)). But it's sadly true that most languages were very very badly done regarding dates (and charsets). – Denys Séguret May 14 '13 at 13:13
  • @dystroy aye, I should have added "popular" – soulcheck May 14 '13 at 13:15
  • It's a matter of the ECMAScript specification. http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.2 . Not sure if they have changed this in later versions... – Menelaos May 14 '13 at 13:30
  • @meewoK So, are there any other core functions which cannot called their methods without creating object or it's the only one. – Ashish Rawat May 14 '13 at 13:34
  • @ ashish2expert I searched the whole spec for the phrase `is not equivalent to the object creation expression` and did not find anything else. Seems Date() might be the only case. – Menelaos May 14 '13 at 13:37

3 Answers3

11

ECMAScript Language Specification

According to the ECMAScript specification (on which Javascript is based):

When Date is called as a function rather than as a constructor, it returns a String representing the current time (UTC).

NOTE The function call Date(…) is not equivalent to the object creation expression new Date(…) with the same arguments.

Reference: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.2

Calling Constructor vs Calling Function

You need the new because you are creating a new Date object. Calling simply Date() , means calling a function that returns the Date() as a string.

See: http://www.javascripture.com/Date

Date() : String
Returns a string representation of the current date and time.

In the case of other types such as Array or Error, the functions are factory functions that create a new object and return them.

See:

Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • I'm not sure you add anything to what's yet in the question. But to be fair I don't really see what's missing to OP. – Denys Séguret May 14 '13 at 13:09
  • It's a matter of spec. Many methods such as `Error()` or `Array()` are actually factory methods that `return new Error();` or `return new Array();` In the case of Date() it doesn't work like that. – Menelaos May 14 '13 at 13:13
  • 1
    +1 for explaining the difference in a clear and concise way, together with references. Nice answer! :) – Filippos Karapetis May 21 '13 at 14:45
3

It is perfectly valid for a JavaScript constructor function to act differently when called with new or without. This is the case of the Date function which returns the date as a string when called without new and as a full fledged object when called with new.

HBP
  • 15,685
  • 6
  • 28
  • 34
1

The point in using new is to create an instance inheriting from Date's prototype.

That's what makes possible that the object can be the receiver of Date's functions.

When you use Date() (which is in my opinion a useless function), you're really getting a string which is the equivalent of (new Date()).toString(). Of course this object only has string functions, not the one of Date.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758