Doug,
Thank you for your reply, very much appreciated.
My understanding of the "new" operator is that it performs the following tasks:
- It creates an object with no members.
- It calls the constructor for the object ( Car in my example above). It then passes a pointer to the newly created object as the "this" pointer.
The constructor then initialises the object. In the example above, it will give the car1 object a color and a type [of vehicle]. Am I correct to assume that this means; car1.color will be 'blue' and car1.type will be 'SUV'.
Car has been passed a reference to a newly created, "empty" object as the value of the "this" keyword. Car is then responsible for initialising the car1 object.
Quote:
|
quote:car1 is not a reference to an object that behaves like "this"
|
Sorry, this isn't what I meant. I think that for a function to be classified as a constructor (15.3.2, ECMA-262, 3rd edition) in JavaScript; it needs to be
- Invoked by the new operator
- Passed the address of a newly created object through the "this" keyword.
Is it incorrect to assume that car1 is this reference? If not, then how does the "new" operator pass the address of the new object to the constructor?
It is very likely that my understanding is still
incorrect.
Any comments are very welcome and greatly appreciated.
Many thanks,
YMas
P.S. Charlie, thank you for your clear explanation of loose-typing.