Skip to content Skip to sidebar Skip to footer

How To Redefine Object And Communicate Object?

I faced some problems, I know var app = app || {} means creating variable app is empty object then app is used through redefining activity. But i don't understand how to use the e

Solution 1:

The var app = app || {}; technique is just a namespacing pattern to avoid polluting the global namespace.

More details on this namespacing pattern

A single global variable named app is created, then the code for the app is added to it. When splitting the app in multiple files, you want to be able to use the app variable if it was defined previously.

Enters the app || {} trick which will return the app variable if it's truthy or the {} empty object if app is falsy (probably undefined).

Though this technique enables different ordering of the files, if a file requires another component (like app.collection), the files should be ordered accordingly.

<scriptsrc="collections.js"></script><!-- defines app.collection --><scriptsrc="views.js"></script><!-- requires app.collection -->

How do I declare a namespace in JavaScript?


Regarding refactoring, first take a look at how to use _.template, depending on the version of Underscore you're using, it might not work as you expect.

Then, for the repeating render function, it's totally normal to have those. They look similar, but will become much different over the development process.

Post a Comment for "How To Redefine Object And Communicate Object?"