Drycodez Posted September 1, 2011 Share Posted September 1, 2011 What is/are the diference between calling a function with the NEW keyword and without the NEW keyword? a=new Object();b=Object();a.txt='string1';b.txt='string2'; The code above produce the-same result. What is/are the diference? Link to comment Share on other sites More sharing options...
jeffman Posted September 1, 2011 Share Posted September 1, 2011 If you use the built in object constructor called Object() (as in your example) I think there is no difference. If you create a unique object, your constructor function will need a return statement to be used without the new keyword. Link to comment Share on other sites More sharing options...
Drycodez Posted September 1, 2011 Author Share Posted September 1, 2011 If you use the built in object constructor called Object() (as in your example) I think there is no difference. If you create... without the new keyword. c=Object;c.txt='string3' what about the above? why is it that no brackets preceed it? Link to comment Share on other sites More sharing options...
Don E Posted September 1, 2011 Share Posted September 1, 2011 There's also a faster way to create objects:var cars = {BMW:"M3",Dodge:"RAM",Mercedes:"CLK"};I believe the above is known as 'literal' way of doing it? c=Object;c.txt='string3' what about the above? why is it that no brackets preceed it? Link to comment Share on other sites More sharing options...
Drycodez Posted September 1, 2011 Author Share Posted September 1, 2011 Yap, i know about literal... Thanks alot! Link to comment Share on other sites More sharing options...
jeffman Posted September 1, 2011 Share Posted September 1, 2011 c=Object;c.txt='string3' It's not what you think. The first statement creates a function called c. It is identical to the function called Object. Because a function is also an object, you can arbitrarily assign it properties. That is what you have done in the second statement. The result is a copy of the Object function with a property called txt. If you try this: alert(typeof c) The alert will print "function" not "object" Link to comment Share on other sites More sharing options...
justsomeguy Posted September 1, 2011 Share Posted September 1, 2011 After doing that, you can also do this, which will create a new object: var obj = new c(); var c=Object;var obj = new c();alert(typeof c);alert(typeof obj); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.