There are two main ways:
Option 1
function MyObject() {
}
var object = new Object();
Option 2
var object = {
};
In general, you can use modules:
var app = app || {};
app.module = ( function () {
return {}
} )();
If you are creating jQuery plugins though, I would follow the jQuery plugin format:
+function () {
var module = function () {};
function Plugin( selector, callback, option ) {
// ...
}
var old = $.fn.module
$.fn.module = Plugin
$.fn.module.Constructor = module
// WAITFOR NO CONFLICT
// ===================
$.fn.waitFor.noConflict = function () {
$.fn.module = old
return this
}
}();
== compares with casting, === compares values and types.
null
is an object with no value.
undefined
is a type.
Powerful MVC frameworks for JavaScript exist, namely backbone.js and angular.js.
They help:
- organize
- maintain
- create single-page apps
- create reusable components
AlreadyDefinedClass.prototype.newFunction = function () {}