Skip to content

Instantly share code, notes, and snippets.

@cfleschhut
Created March 28, 2011 14:00
Show Gist options
  • Save cfleschhut/890503 to your computer and use it in GitHub Desktop.
Save cfleschhut/890503 to your computer and use it in GitHub Desktop.
ejohn.org “Advanced JavaScript” Code Examples
/* Chapter 14: Enforcing Function Context */
// http://ejohn.org/apps/learn/#86
Function.prototype.bind = function() {
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function() {
return fn.apply(object, args.concat(Array.prototype.slice.call(arguments)));
};
};
// function bind(context, name) {
// return function() {
// return context[name].apply(context, arguments);
// };
// }
var Button = {
click: function(value) {
this.clicked = value;
}
};
var elem = document.createElement("div");
elem.innerHTML = "Click me!";
elem.onclick = Button.click.bind(Button, true);
document.body.appendChild(elem);
elem.onclick();
console.log(Button.clicked);
/* Chapter 12: Inheritance */
// http://ejohn.org/apps/learn/#78
function Person() {}
Person.prototype.getName = function() {
return this.name;
};
function Me() {
this.name = "Manfred";
}
Me.prototype = new Person();
var me = new Me();
me.getName();
/* Chapter 9: Temporary Scope */
// http://ejohn.org/apps/learn/#62
var count = 0;
for(var i=0; i<4; i++) (function(i) {
setTimeout(function() {
console.log(i);
}, i* 250);
})(i);
/* Chapter 8: Closures */
// http://ejohn.org/apps/learn/#52
var count = 0;
var timer = setInterval(function() {
if(count <= 5) {
console.log(count);
count++;
} else {
clearInterval(timer);
}
}, 250);
// http://ejohn.org/apps/learn/#53
var count = 1;
var elem = document.createElement("div");
elem.innerHTML = "Click Me!";
elem.onclick = function() {
console.log(count++);
};
document.body.insertBefore(elem, document.body.firstChild);
// http://ejohn.org/apps/learn/#54
function Ninja() {
var slices = 0;
this.getSlices = function() {
return slices;
};
this.slice = function() {
slices++;
}
}
var ninja = new Ninja();
ninja.slice();
ninja.getSlices() == 1;
/* Chapter 7: Flexible Arguments */
// http://ejohn.org/apps/learn/#40
function merge(root) {
for(var i = 0; i < arguments.length; i++) {
for(var key in arguments[i]) {
root[key] = arguments[i][key];
}
}
return root;
}
merge({a: 1}, {b: 2}, {c: 3, d: 4, e: 5});
// http://ejohn.org/apps/learn/#41
Array.prototype.min = function() {
return Math.min.apply(Math, this);
}
Array.prototype.max = function() {
return Math.max.apply(Math, this);
}
[222, 333, 111].min();
[222, 333, 111].max();
// http://ejohn.org/apps/learn/#45
function highest() {
return Array.prototype.slice.call(arguments).sort(function(a, b) {
return b - a;
});
}
highest(99, 1111, 222)[0];
// http://ejohn.org/apps/learn/#46
function multiMax(multi) {
var allButFirst = Array.prototype.slice.call(arguments, 1);
var largestAllButFirst = Math.max.apply(Math, allButFirst);
return multi * largestAllButFirst;
}
multiMax(3, 1, 2, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment