Skip to content

Instantly share code, notes, and snippets.

@stevobengtson
Last active August 29, 2015 14:08
Show Gist options
  • Save stevobengtson/16a89a194055f7cce11d to your computer and use it in GitHub Desktop.
Save stevobengtson/16a89a194055f7cce11d to your computer and use it in GitHub Desktop.
Javascript Name Spaced Object
// Example of creating a Name Spaced object in javascript inspired by:
// http://stackoverflow.com/questions/881515/how-do-i-declare-a-namespace-in-javascript
// Jaco Pretorius: http://stackoverflow.com/users/121531/jaco-pretorius
(function( skillet, $, undefined ) {
//Private Property
var isHot = true;
//Public Property
skillet.ingredient = "Bacon Strips";
//Public Method
skillet.fry = function() {
var oliveOil;
addItem( "\t\n Butter \n\t" );
addItem( oliveOil );
console.log( "Frying " + skillet.ingredient );
};
//Private Method
function addItem( item ) {
if ( item !== undefined ) {
console.log( "Adding " + $.trim(item) );
}
}
}( window.skillet = window.skillet || {}, jQuery ));
//Adding New Functionality to the Skillet
(function( skillet, $, undefined ) {
//Private Property
var amountOfGrease = "1 Cup";
//Public Method
skillet.toString = function() {
console.log( skillet.quantity + " " +
skillet.ingredient + " & " +
amountOfGrease + " of Grease" );
console.log( isHot ? "Hot" : "Cold" );
};
}( window.skillet = window.skillet || {}, jQuery ));
// Now you can use it
skillet.addItem('Eggs');
skillet.fry();
skillet.toString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment