Last active
December 23, 2015 14:49
-
-
Save dustintheweb/6651784 to your computer and use it in GitHub Desktop.
Various var & function syntax used in JS & jQuery
This file contains hidden or 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
// Var & Function Syntax // WIP | |
var someName; // a variable on the local scope | |
someName; // a variable on the global scope | |
window.someName; // a production friendly variable on the global scope | |
// note: when making a reference to any var with a specific namespace, | |
// make sure you specify it in the call | |
$.someName; // a variable on the global scope | |
// (jQ equivalent of window.someName) | |
$.gv = { // a unique namespace for a collection of variables on the global scope | |
someName1 : '', // gv can be anything | |
someName2 : '', // more info: http://goo.gl/oTRhhk | |
someName3 : ''; | |
}; | |
$.fn.someName; // a function stored as a variable on the global scope | |
// (fn is not needed, but it is used for quick delineation in code & grouping in the DOM) | |
var $someName; // a variable on the local scope | |
// ($ is not needed, added only for quick delineation between the stored object type // DOM object) | |
var $someName1; // a stack of var declarations | |
var $someName2; | |
var $someName3; | |
var $someName1, // a more efficient stack of var declarations | |
$someName2, | |
$someName3; | |
////////// | |
function = someFunc() { | |
// this is a typical function | |
// do something | |
}); | |
someFunc(); // call it | |
// ------- | |
var someFunc = function() { | |
// this is called a named function & saves it as a variable (more extensible) | |
// do something | |
}; | |
$.fn.someFunc(); // call it | |
// now its a global var w/ global scope | |
$('#myId').MyFunction(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment