February 2012
3 posts
3 tags
Function.prototype.bind
Modernizr 2.5 added lots of robust feature detects and tests, but perhaps its most convienent feature is the addition of a Function.prototype.bind polyfill. In short, .bind() allows you to set this within a function.
As I employ prototypal objects and class methods a lot, I’m using .bind() a bunch. It’s helpful within setTimeout:
function ClassAct() {
this.generation = Math.floor(...
2 tags
Slicing arguments
In the jQuery Plugins Authoring tutorial, Ralph Holzmann details an intriguing pattern for plugin methods:
var args = Array.prototype.slice.call( arguments, 1 );
return methods[ method ].apply( this, args );
// (edited for clarity)
As the tutorial explains, this pattern is what enabled jQuery UI plugins to have multiple methods. Indeed, If you look deep within the coils of jQuery UI widget...
3 tags
Varry var var
John Schulz turned me on to the code style of using var for every variable. This goes against most other code styles (see learn.jquery.com and Idiomatic JavaScript), which advise using one var, and listing following variables with commas.
// typically recommend
var firstName = 'Ryan',
lastName = 'Gosling',
age = 31,
isAlive = true;
// multiple vars
var firstName = 'Ryan';
var...