Date()-a function or a method?

Hello!
date() is obviously not a method, is it?
Please, where does it nelong then?
Many thanks!

Why do you say date() is not a method? What reasons do you have for providing that position?

Not saying you’re right or wrong, but why are you stating that position?

I hope I get this right, but I’m pretty sure I’m correct. (Someone correct me if I’m wrong). If you’re doing OOP then a method can be considered a function, but a function isn’t’ necessary a method (Think procedural). However I see it where most people intermingle both constantly on forums and even in some tutorials, so that is just splitting hairs?

Yeah I’d basically agree with that; To my mind the only distinction is that a method is a function that is attached to a class/object… although even that distinction doesn’t really make sense in JS, as all functions belong to an object (functions in the global scope belong to the window object).

Are you talking about the Date function or the Date object? Your question doesn’t make it clear which you are asking about.

You call the object using new and the function without using new

For example the following two statements return the same thing - the first using the Date object and the second using the Date function:

console.log((new Date()).toLocaleString());
console.log(Date()); 

The Date function can be considered a method of the window object.

Date() is a function and a method. A function because it can be called using (), it will perform an action and it will return a value. A method because it is attached to an object, the global window object.

You get and create a date object using the new operator and you execute a call that returns a string without the new operator.

Given someFunc, is it a function, a method, or a constructor? Honestly, the answer is N/A. A function in JavaScript could be any of those depending on how it’s invoked.

// invoked as a function
someFunc();

// invoked as a constructor
new someFunc();

// invoked as a method
someObject.someFunc();

// invoked as a method?
someFunc.call(someObject);

In all these cases, the only difference is what the value of this will be during that particular invocation. When invoked as a function, this will be the global object (or undefined if in strict mode). When invoked as a constructor, this will be a brand new object. When invoked as a method, this will be the object on which the method was invoked. And when invoked with call or apply, this is set manually.

So what is Date? Depends on each invocation.

Hello!
It is my first touch with JS and it is the very first w3school example:

I was just curious, cause I thought it might be a metod, googled js methods< but found just this new date() and not only date()

As I did not know the difference, I mailed here.
Hope U would understand.
Many tutorials are acutally a bit “advanced”.
I concluded myself that date() might be a method (try, and see!, what hapens!)

But, when I read it somehere, I just feel more sicure.
Appologise if I made too much wind!

The two guys that own that site have recently updated the first few JavaScript examples to b ring them into the 21st Century but most of their JavaScript is still mainly for Netscape 4 and earlier and is yet to be modernised. They have too many topics to keep them all up to date properly.

If i don’t misunderstand. new Date() ?. It’s object :smiley: and have a lot of function in it, check this out:
_ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

Yes that is an object

Date() without the new is a function and does something completely different.

I don’t think you’re drawing the right distinction. Date, regardless of whether you use “new”, is a function, and therefore also an object. Like any function, there’s a variety of ways you can invoke it. If you invoke it with “new”, then you’re invoking it as a constructor function.

1 Like

Date does two completely things depending on whether you invoke it with or without ‘new’.

Without new it returns a string containing the current date and time in local format.

With new it returns a Date object.

You’re talking about the return value of Date. I’m talking about Date itself – or at least I was 10 months ago. :wink:

1 Like

I thought posts were supposed to close automatically if there were no replies within three months. I wonder how this one stayed open for ten months?

Off-Topic

The auto-close 3 months after the last reply wasn’t put in place IIRC ~late October.
Many older dormant discussions were closed manually, but some were missed.
Some members seem to have a knack for finding them, ignoring the “revive?” modal and resurrecting them.

In this case the person who added the first comment after 10 months has added nothing significant to the conversation and just confused me as to how long ago Jeff made his last comment. Perhaps the recent comments should be removed and this post closed as it should have been long ago.

1 Like

You should visit marketing/SEO on this forum since it seems no posts can ever close due to every single old thread gets revived after a month or two with worthless fluff posting.

Surely once those are removed the date of the last post ought to revert back to the last meaningful one so that the thread can close three months after the last MEANINGFUL post.

You’d think so. But that is a “feature” of Discourse…among many more “features”…