MS said:
My understanding is that you use the NEW keyword when creating
instances of objects,
To be terminologically exact (which is a good idea when trying to
understand something - it's safer to get lazy when you understand
what you are talking about

, you create *objects*. In other, class
based, languages, objects are instances of classes. Javascript
does not have classes, so "instance" is a word to avoid. It only
confuzes. "Instance of object" is overdoing it
since you cannot change the properties of the
Date object directly, for example.
The "Date" object (i.e., the function object accessible through
the global property "Date") is used as a constructor function.
It doesn't have any value to change.
You can think of constructor functions as somewhat similar
to classes, but the similarity is only skin deep. They are
closer to initializer/constructor-methods of a class than to
classes themselves. In Javascript they initialize new objects
with values, but the objects themselves have no type/class.
The "date object" that you create using "new Date()" is completely
different. It is not a function, but an object that represents
a point in time.
You use the "new" operator with a constructor function to
create a new object. The constructor function initializes
the new object, and the object accessible through the
"prototype" property of the constructor function object
(functions are objects) is used as the inheritance chain
of the new object - i.e., the properties of this "prototype
object" can be read as properties of the new object unless
shadowed by properties directly assigned to the new object.
If you want to change the value of the Date object, you need to
create an instance of it, through a variable and NEW keyword.
The Date constructor function doesn't have a value at all, so
there is nothing to change. The date objects you create from
it do have a time value, and it can be changed.
I guess part of my problem is what are the objects you need to use the
NEW keyword for? I know DATE() is one of them but is there a list
somewhere that can be used as a reference?
You can use "new" with any function, using it as a constructor
function. Some functions have been created with that use in mind. In
the ECMAScript language, this includes the functions available through
the global variables named:
Object
Array
String
Number
Boolean
Function
RegExp
Date
Error
For Object, Array, Function and RegExp, the language have notation
for creating new objects besides using "new" (being: {}, [...],
function(...){...}, and /.../).
You only need to use "new" with these types for backwards compatability
with old versions of Javascript before the literal syntax was introduced.
I have yet to need to create an object from String, Number and
Boolean. These objects are different from the simple type values
they wrap, i.e., "new Number(4)" is an object where "4" is a number.
Javascript automatically creates a new wrapper object when using
property accessor notation, so
"abc".substring(1,1)
is equivalent to
new String("abc").substring(1,1)
The only case where I can see creating a new object explicitly to
be useful is if such a property access is inside a tight loop
and always on the same string (or other value). The creating the
object outside the loop avoids creating a new object implicitly
for each iteration.
This leaves only Date and Error, and I haven't used Error for anything
yet

. Also, "Error()' is equivalent to "new Error()", so "new" is
not needed (the same applies to RegExp and Function, which also create
new objects when called as a function).
Am I accurate in what I wrote here?
Close, but it can always be more accurate (just get a pedant like me
started
I use "instance" myself when talking about objects created from a
constructor function, i.e., "an instance of Date" instead of
"an object created by 'new Date()'", but that *is* being lazy.
/L