Forked from stevobengtson/gist:16a89a194055f7cce11d
Last active
August 29, 2015 14:19
-
-
Save dgreenway/d96eb8263cff570711f5 to your computer and use it in GitHub Desktop.
Namespaced js object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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