Date.getTime() v.s. Date.now()

javascript

Solution

It's the difference between properties of the constructor object and properties of the constructor object's prototype. The "now" property is a property of the Date constructor itself, and not a property of `Date.prototype`. It's the opposite situation for "getTime".

Semantically it makes sense: the concept of "now" is independent of any particular date instance. The "getTime" method is intended to report on the timestamp for the date actually represented by a particular date instance.

If you're defining your own constructors, you can create "class methods" (I personally would hesitate to call them that, but whatever) like this:

function MyConstructor() {
  // ...
}

MyConstructor.someMethod = function() {
  // ...
}

Then `MyConstructor.someMethod()` calls that function independently of any particular instance of your class.

Problem

I noticed that now() can only be called by the Date object. getTime() can only be called by an instance of date. ``` var dd1 = new Date(); //console.log(dd1.now()); //Throws error -> TypeError: Object Mon Aug 19 2013 16:28:03 GMT-0400 (Eastern Daylight Time) has no method 'now' console.log(dd1.getTime()); console.log(Date.now()); //console.log(Date.getTime()); //Throws error ->TypeError: Object function Date() { [native code] } has no method 'getTime' ``` Is there a formal name for this difference? Is this the difference between "static" and "non-static." When I create a new instance of Date, shouldn't all methods be inherited?

Original source