Jump to content

Obj1=new Object()


Drycodez
 Share

Recommended Posts

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?

Edited by O. Samuel
Link to comment
Share on other sites

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...