Skip to content

Instantly share code, notes, and snippets.

View lennym's full-sized avatar

Leonard Martin lennym

View GitHub Profile
@lennym
lennym / gist:703272
Created November 17, 2010 11:09 — forked from avhm/gist:703244
$.expr[':'].onscreen = function(obj){
var $win = $(window), pos = $(obj).position();
return $.contains(document.documentElement, obj) && pos.top < $win.height() && pos.left < $win.width();
};
$.expr[':'].offscreen = function(obj){
return !$(obj).is(':onscreen');
};
@lennym
lennym / gist:1164645
Created August 23, 2011 08:27
Object passing by reference in Backbone.Model default values.
var TestModel = Backbone.Model.extend({
defaults: {
primitiveProp: 'foo',
objectProp: {
prop1: 'val',
prop2: null
}
}
});
@lennym
lennym / gist:1270328
Created October 7, 2011 13:50
Odd arguments behaviour in Google Chrome
function foo (val) {
console.log(arguments, arguments[0]);
val = 2;
}
foo(1);
// logs "[2], 1" in Google Chrome
@lennym
lennym / jcc.js
Created November 18, 2011 22:57 — forked from EddieDev/jcc.js
Javascript Code Challenge
Array.prototype.joinProperty = function(delim,method) {
var output = '';
for(var i=0; i < this.length; i++){
if(this[i].hasOwnProperty(method)){
output = output.concat(this[i][method],delim);
}else if(eval("typeof this[i]."+method+" != 'undefined'")){
output = output.concat(eval('this[i].'+method+'()'),delim);
}
}
@lennym
lennym / jcci.js
Created November 18, 2011 23:03 — forked from EddieDev/jcci.js
Javascript Code Challenge and Implementation. http://eblundell.com/javascript-challenge
Array.prototype.joinProperty = function(method, delim) {
var output = [];
for(var i=0; i < this.length; i++){
if(typeof this[i][method] == 'function'){
output.push(this[i][method]());
} else {
output.push(this[i][method]);
}
}
return output.join(delim);
@lennym
lennym / gist:2153939
Created March 21, 2012 22:53
Example of object and function based variable scope.
/** Example of Object based scope **/
ExampleObject = {
setVariable: function () {
this.value = 'foo';
},
getVariable: function () {
// this.value is accessible from here because it was declared as a property of "this"
return this.value;
}
@lennym
lennym / gist:3091497
Created July 11, 2012 16:14 — forked from rmurphey/gist:3086328
What's wrong with Netmag's "Optimize your JavaScript" post

What's wrong with Netmag's "Optimize your JavaScript" post

I tweeted earlier that this should be retracted. Generally, these performance-related articles are essentially little more than linkbait -- there are perhaps an infinite number of things you should do to improve a page's performance before worrying about the purported perf hit of multiplication vs. division -- but this post went further than most in this genre: it offered patently inaccurate and misleading advice.

Here are a few examples, assembled by some experts in the field (largely Rick Waldron and Ben Alman, with some help from myself and several others from the place that shall be unnamed).

Factual inaccuracies

  • Calling array.push() five times in a row will never be a "performance improvement." The author has clearly confused creating an array literal `["foo", "
@lennym
lennym / gist:3361176
Created August 15, 2012 15:55
How to get an SVG file inline in a page in IE9
iframe = document.createElement('iframe');
iframe.src = asset.svg;
iframe.onload = _.bind(function () {
this.$el.append(iframe.contentDocument.getElementsByTagName('svg')[0]);
document.body.removeChild(iframe);
ready();
}, this);
document.body.appendChild(iframe);
@lennym
lennym / gist:3825800
Created October 3, 2012 08:29
Require JS oddness
// this works
define(function (require) {
var a = require('a');
return a;
});
// this fails, and I have no idea why
define(function (require) {
var a = 'a';
var b = require(a);
@lennym
lennym / facebook_user.js
Created October 12, 2012 13:00
Node script for making test users for facebook apps.
/**
Usage: node facebook_user.js <app_id> <app_secret>
**/
var http = require('https');
var APP_ID = process.argv[2];
var SECRET = process.argv[3];
var NAME = process.argv[4] || 'Test%20User';
if (!APP_ID || !SECRET) {
throw new Error('App ID and secret must be defined');