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 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.
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.
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.
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ââŚ