-1

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"};

?

2
  • you shouldn't think about that too much, you should just code in javascript(the way you want), and in some moment you will see, that the first one can become useful. Commented Jan 6, 2013 at 11:36
  • 3
    @Dupadupa Why should he not think about it? Commented Jan 6, 2013 at 11:37

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.