I understand the difference in behavior. Date()
returns a String representing the current date, and new Date()
returns an instance of the Date object whose methods I can call.
But I don't know why. JavaScript is prototyped, so Date
is a function and an object which has member functions (methods) which are also objects. But I haven't written or read any JavaScript that behaves this way, and I'd like to understand the difference.
Can somebody show me some sample code of a function that has a method, returns an instance with the new operator, and outputs a String when called directly? i.e. how does something like this happen?
Date(); // returns "Fri Aug 27 2010 12:45:39 GMT-0700 (PDT)"
new Date(); // returns Object
new Date().getFullYear(); // returns 2010
Date().getFullYear(); // throws exception!
Very specific request, I know. I hope that's a good thing. :)
The Date object is an inbuilt datatype of JavaScript language. It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.
The new keyword is used in javascript to create a object from a constructor function. The new keyword has to be placed before the constructor function call and will do the following things: Creates a new object. Sets the prototype of this object to the constructor function's prototype property.
New keyword in JavaScript is used to create an instance of an object that has a constructor function. On calling the constructor function with 'new' operator, the following actions are taken: A new empty object is created.
An instance is an object containing data and behavior described by the class. The new operator instantiates the class in JavaScript: instance = new Class() . For example, you can instantiate the User class using the new operator: const myUser = new User(); new User() creates an instance of the User class.
Most of this is possible to do yourself. Calling the bare constructor without new
and getting a string is special for Date
per the ECMA spec, but you can simulate something similar for that.
Here's how you'd do it. First declare an object in the constructor pattern (e.g. a function that is intended to be called with new
and which returns its this
reference:
var Thing = function() {
// Check whether the scope is global (browser). If not, we're probably (?) being
// called with "new". This is fragile, as we could be forcibly invoked with a
// scope that's neither via call/apply. "Date" object is special per ECMA script,
// and this "dual" behavior is nonstandard now in JS.
if (this === window) {
return "Thing string";
}
// Add an instance method.
this.instanceMethod = function() {
alert("instance method called");
}
return this;
};
New instances of Thing can have instanceMethod()
called on them. Now just add a "static" function onto Thing itself:
Thing.staticMethod = function() {
alert("static method called");
};
Now you can do this:
var t = new Thing();
t.instanceMethod();
// or...
new Thing().instanceMethod();
// and also this other one..
Thing.staticMethod();
// or just call it plain for the string:
Thing();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With