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() {}
// 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();
}
class JsObject(object):
def __init__(self, *args, **kwargs):
for arg in args:
self.__dict__.update(arg)
self.__dict__.update(kwargs)
def __getitem__(self, name):
return self.__dict__.get(name, None)