Is "new" the only way to initialise an object in Javascript?
I just came across this code:
Code:
that=DHTMLSprite(params) ; Code:
that.moveanddraw = function() { ...whatever I can't understand it. Isn't there supposed to be a "new" before "DHTMLSprite"? Without it I would have thought that the variable "that" would contain the result of calling the function "DHTMLSprite" with "params" as the parameter.....? Thanks. |
Quote:
It's also what would happen if DHTMLSprite is called with new. Calling a function with "new" just sets that function's "this" variable to a special value. It's not like C++, Java or C#. |
I don't follow you at all. Without "new", there is NO OBJECT. So how can "that.moveanddraw = function() { ...whatever" work?
I'm ignoring the 2nd part of your post for now. I don't know C++, Java or C#, so they have no influence on my work. |
I am far from expert on JavaScript, nevertheless I'll try to explain, and it's also a good opportunity to refresh my memory on this topic.
DHTMLSprite in your example could be returning an object as follows: Code:
function DHTMLSprite(params) { Here's how new operator works: it creates a new object which inherits from provided function's prototype. Then that function is called and this inside it is bound to the newly created object. You can modify this by assigning any values to it and even new functions. Note that normally the function used with new does not return a value (though it can, but I'm not sure about the details myself). Code:
function DHTMLSprite(params) { Hopefully, I have made things clearer and haven't introduced more confusion to the topic. :) |
Quote:
Okay... Wherever you got this information, it's not correct. I'd be curious to see a link to it. Accurate information has been provided by audriusk in this thread, and by me in the thread that you started specifically to ask how the new operator works. |
Quote:
So, in that DHTMLSprite example, there's an object being returned in the "DHTMLSprite" function? (I'll have to go check my book). Quote:
OK, this is beyond me for now. I don't really follow what you're saying. But anyway, I'll try and keep it in mind for when my Javascript skills are a bit more than what they are now :) Oh by the way I followed your second code example just fine :) |
Quote:
Actually in my PAPER book, Javascript Cookbook, it's said that a function can either be made an object of, with new, or can be called as a function. So I naturally got the idea that new was the only way to create an object..... |
Quote:
Quote:
Code:
function foo() { And here's the simplest way to create an object: Code:
foo = {}; |
Quote:
No, you were right. Inside DHTMLSprite there was this: Code:
and later, a Code:
return that; I'm guessing "that" is an object. What is the notation they've used above (draw: function)? Is there any webpage exlaining it? I'm not familiar with it.... Quote:
Thank God you're closer to my time zone, and I can get the answer quicker than from Dugan :) Btw, what's a good book to learn all the stuff you're talking about? |
Quote:
|
Oh hey man, you're up and about :)
What time is it for you right now? |
7:30 am. I'm in the west coast of Canada.
|
Dugan's recommended book is really good. Or at least should be, cause I haven't read it, but I know the author. The guy understands the language really well. When I started using JavaScript for work, my then boss introduced me to Crockford's writings on the net and JSLint tool created by him. There's a concise overview of the language on his website. Probably not the easiest read for beginner, but good nonetheless. It should answer a lot of your questions from this thread (or raise even more :)).
Quote:
Code:
foo = { Quote:
Code:
// This for-loop iterates over all the keys in foo object and its prototype chain. |
Yes, I know of Douglas Crockford too, I downloaded some videos of his on JS from Yahoo YUI theater some years back. Couldn't follow them though, so just left them, and bought some books instead, for the time being. Will go back to the videos, hopefully soon.
I'm still wavering on the notation used - so an object can be defined either as "foo = { bar:42" OR "var x = function foo() { this.bar = 42} "? Is the former, the one you've explained above, what they call JSON? Thanks for all your help. |
Quote:
Code:
var foo = { Code:
// Note the capitalized function name, this is often used to |
All times are GMT -5. The time now is 02:10 AM. |