user1952709
user1952709

Reputation: 1

create object in javascript

What is the difference between that

    install=function(s,p){var n;for(n in p)if(p.hasOwnProperty(n))s[n]=p[n]}}
    install(myObj,{userid:"5",username:"john"});

To

    myObj={userid:"5",username:"john"};

?

Upvotes: -1

Views: 80

Answers (2)

gustaf r
gustaf r

Reputation: 1234

You should try to clean-up the code a bit white-space-wise to make it easier to read.

Well, in the first case, you never really create the object myObj. So I guess that code doesn't even run.

The second code is how you usually construct anonymous objects.

Upvotes: 0

asgoth
asgoth

Reputation: 35829

The first is using a function, where it (probably) will copy the properties to myObj. You will mostly use this when you need to add properties to an already existing object.

The second just creates a new object with the given properties.

Upvotes: 2

Related Questions