Skip to content

Instantly share code, notes, and snippets.

View bcherry's full-sized avatar

Ben Cherry bcherry

View GitHub Profile
var mycallback;
function foo() {
var x, y, z;
function bar() {/* something with x, y, and z*/}
mycallback = bar;
};
NaN === NaN; // false
NaN + 1; // NaN
NaN = 1;
NaN === NaN; // true
NaN + 1; // 2
typeof undefined; // "undefined"
undefined = 1;
typeof undefined; // "number"
var ids = ["elem1", "elem2", "elem3"...],
i,
l = ids.length;
for (i = 0; i < l; i += 1) {
(function (i) {
$("#" + ids[i]).bind("click", function () {
alert(i);
});
}());
var MYGLOBAL = (function () {
var that = {},
privateVariable = 2;
function privateFunction() {
//...
}
that.publicFunction = function () {
//
var foo = {
bar: "foobar",
func: function () {
return this.bar;
}
};
foo.func(); // "foobar"
var functionReferences = {};
function foo(one, two, three) {
one = one || "defaultvalue"; // assigns default value if one is falsy (null, undefined, 0, "0", false, etc.)
two = two == null ? "defaultvalue" : two; // assigns default value if 'two' is not null or undefined (so you can pass things like 0 or false)
three = three === undefined ? "defaultvalue" : three; // assigns default value if three is not undefined (so you can pass null)
}
var module = (function () {
var my = {},
privateStaticModuleVariable = "something";
my.publicStaticObjectConstructor = function () {
var that = {},
privateInstanceVariable = "something";
that.publicInstanceVariable = "something";
function privateInstanceFunction() {}
var performance = (function () {
var my = {};
// Wrap a function body in this to return a copy that instruments itself
// If you want this to be useful, you should give your profiled function a name,
// otherwise it will be identified as "", which is less than useful.
my.profile = function (func) {
return function () {
var start = new Date().getTime(),
time,
// Simple arithemetic parser.
// Follows order of operations, only supports positive integers
// Only operators are +, -, *, and /. Does not support grouping with ()
var calc = (function () {
var calc,
sum,
negasum,
product,
dividend;
var foo, bar;
foo = function () {
bar();
}
bar = function () {
foo();
}