Skip to content

Instantly share code, notes, and snippets.

@roundcorners
roundcorners / Borrowing toString
Created October 21, 2011 11:56
Testing for Array borrowing the toString method from Object object
Object.prototype.toString.call([]) === "[object Array]"
@roundcorners
roundcorners / function-application.js
Created October 24, 2011 06:37
Applying a function
myObject.getName.apply(otherObject, [args]);
@roundcorners
roundcorners / borrowing-slice.js
Created October 24, 2011 06:47
Invoke slice as a method of arguments
Array.prototype.slice.call(arguments, 1);
@roundcorners
roundcorners / basic-iterator.js
Created October 24, 2011 15:14
The Iterator!
// Our iterator
var iterator = (function() {
var index = -1,
// Our data
data = ['joe', 'chapman', 'developer', 'UK'],
// cache the length
length = data.length;
return {
@roundcorners
roundcorners / count.js
Created October 25, 2011 09:20
Get an object's length
var count = function(obj) {
var size = 0, key;
for (key in obj) {
size++;
}
return size;
};
@roundcorners
roundcorners / getItem.js
Created October 25, 2011 09:21
Return an object's key value based on the position of the key
var getItem = function(obj, i) {
var key, arr = [];
for (key in obj) {
arr.push(obj[key]);
}
return arr[i];
};
@roundcorners
roundcorners / updated-next.js
Created October 25, 2011 09:32
Updated iterator object
length = data.length ? data.length : count(data);
next: function() {
if (!this.hasNext()) {
return null;
}
index = index + 1;
if (data[index] !== undefined) {
return data[index];
}
@roundcorners
roundcorners / finished-iterator.js
Created October 25, 2011 09:42
Complex iterator object with helpers
var count = function(obj) {
var size = 0, key;
for (key in obj) {
size++;
}
return size;
};
var getItem = function(obj, i) {
var key, arr = [];
@roundcorners
roundcorners / fiddle.response.json
Created November 1, 2011 16:51
basic json for response
var data = {
"package_all" : {
"TV": true,
"Broadband": true,
"Talk": true
}
}
<div>
<p>You selected:</p>
</div>