質問

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.

役に立ちましたか?

解決

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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top