Javascript object initialisation question
If I say "new <function name>" to initialise an object, is the entire code for <function> RUN as part of the initialisation process?
What exactly happens? Thanks. |
<function>() is executed, with "this" set to a new object that inherits from the function's prototype.
And yes, I had to look that up. |
What does that mean? What if there IS nothing defined as a prototype? i.e. var x = new foo() ; is say, the first statement in the program, after the code for foo?
Also, someone (in the know, ostensibly) told me that the code for foo WON'T be executed - it'll only be compiled, and executed later....? |
Quote:
This is probably the best information on the Javascript new keyword you can get for free: https://developer.mozilla.org/en-US/.../Operators/new You really should purchase a copy of "Javascript: The Good Parts", which is my go-to book for things like this: http://shop.oreilly.com/product/9780596517748.do Quote:
Yes, the entire code for the function is run when you call it with "new". I don't know why you or your friend would have thought otherwise. |
All times are GMT -5. The time now is 05:54 PM. |