3

I'm studying objects in JavaScript and confused when the new keyword and () are required when using a object creation expression.

var a = new Date();     // current date and time
var b = Date();         // current date and time
var c = new Date;       // current date and time 
var d = Date;           // => function Date() { [native code] }

Is there any difference in the first three methods? Why doesn't d do as expected?

James
  • 1,873
  • 3
  • 22
  • 28

3 Answers3

3

Given: new Date()

This is the "standard" way to create a new object from a constructor-function1; it returns a new Date object representing the current time.

Given: Date()

JavaScript defines the Date function to operate like this when not called as a constructor (ie. with new). It returns a different value - a string and not a Date object - than new Date(). See Why we can't call methods of Date() class without new operator.

User-code can also check the value of this inside a constructor-function to determine if new was used, although this is only done relatively infrequently. See How to detect if a function is called as constructor?

Given: new Date

When using new the parenthesis are optional if there are no arguments. This is just an additional syntax form that most people don't use - it is equivalent to new Date() if the expression is terminated. See Can we omit parentheses when creating an object using the "new" operator?

Given: Date

This evaluates the expression to the constructor-function (which is just a function-object!2) without invoking it or creating a new instance. This is why the result shows a "function" - it is the constructor-function.


1 I use the term constructor-function to emphasis the point that a constructor is only a function that [also] supports new; it is technically sufficient to call it a constructor.

2 Likewise, it is important to understand that function[-objects]s in JavaScript are first-class citizens and thus just values themselves; like any object. In the last example the assignment of the object/value occurred without performing any action (ie. function invocation) upon the object itself.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
0

Date is a constructor function:

function Date() {}

When you use new you create an instance of Date:

var date = new Date;
date instanceof Date === true

The parenthesis are optional if you don't pass any arguments.

Now, Date on its own, is a reference to that constructor function. Therefore the line below:

var date = Date

Is simply assigning that function to a variable called date. While this other line:

var date = Date()

Is invoking the constructor function. But, without new it doesn't necesasarily mean it will return an instance of Date. This behavior is up to the function to decide; many constructor functions in JavaScript can be called without new. The function might handle it like this:

function Date() {
  if (!(this instanceof Date)) {
    return new Date()
  }
  ...
}
elclanrs
  • 92,861
  • 21
  • 134
  • 171
0

Date is a reference to a function. Simply referencing it doesn't execute it. That's why d acts that way.

new Date and new Date() both return a new instance of the Date object. One just doesn't have the option of passing in arguments.

I think Date() does the same, but this is not standard behavior for constructors. Functions will sometimes return new instances of objects.

function Fun(){ return new Trick(); }

posit labs
  • 8,951
  • 4
  • 36
  • 66