Question

all i have seen creating objects like below, what is this type creating object actually?

var myApp = myApp || {};

I'm creating object like this

var myApp={property:"value",method:function(){}};

Can anybody please tell me the difference between both type.

Était-ce utile?

La solution

The first example won't replace the variable myApp if it already exists, and is useful for working in unfamiliar namespaces or when working with global variables. You will have to specify myApp's properties after creation.

The second example will always create a new version of myApp and allows you to specify its properties during creation. It will override any other variables already named myApp.

Both examples create the same 'type' of object--there is only actually one type of object. What makes objects different from each other is the properties they possess.

Autres conseils

The first example assigns a value to itself if it already has a true value, and an object with no properties otherwise.

The second example assigns an object with properties.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top