space, → | next slide |
← | previous slide |
d | debug mode |
## <ret> | go to slide # |
c | table of contents (vi) |
f | toggle footer |
r | reload slides |
z | toggle help (this) |
var obj = {
first: 1,
second: 2
};
function eachProp(operation) {
operation('first', obj.first);
operation('second', obj.second);
}
eachProp(function(name, value) {
alert('The value of '+ name +' is '+
value);
});
var sum = function(a, b) {
return a + b;
};
var foo = sum;
assert( foo(2, 3) == 5 );
var double = function(a) {
return a * 2;
};
function twice(op, x) {
return op(op(x));
};
assert( twice(double, 2) == 8 );
var array = [1, 2, 3];
var doubled = array.map(function(e) {
return e * 2;
});
assert( doubled[2] == 6 );
var total = array.reduce(function(sum, e) {
return sum + e;
}, 0);
assert( total == 6 );
function mirrors() {
return mirrors.toString();
}
assert( mirrors() ==
"function mirrors() {
return mirrors.toString();
}" );
var x = 3;
var y = 4;
(function a() {
var y = 5;
assert(x == 3);
assert(y == 5);
})();
assert(x == 3);
assert(y == 4);
var y = 4;
for (var i = 0; i < 10; i += 1) {
var y = i;
}
assert(y == 9);
assert(i == 10);
function outer() {
var x = 3;
return function() {
x += 1
return x;
};
}
var inner = outer();
assert(typeof x == 'undefined');
assert(inner() == 4);
assert(inner() == 5);
this
arguments
this
var obj = {
func: function() {
return this;
}
}
assert( obj.func() == obj );
var obj = {
func: function() {
return this;
}
}
var func = obj.func;
assert( func() != obj );
var obj = {
func: function() {
return this;
}
}
var func = obj.func;
// `this` refers to the global object
assert( func() == window );
var obj = {
func: function() {
return this;
}
}
// `this` refers to the receiver
assert( obj.func() == obj );
assert( obj['func']() == obj );
var obj = {
func: function() {
return this;
}
}
// `this` refers to a new object
assert( new obj.func() != obj );
assert( new obj.func() != window );
var obj = {
func: function() {
return this;
}
}
var otherFunc = function() { return this; };
// `this` refers to the given argument
assert( otherFunc.apply(obj) == obj );
assert( otherFunc.call(obj) == obj );
apply()
and call()
function sum(a, b) {
return a + b;
}
assert( sum.apply(null, [1, 2]) == 3 );
assert( sum.call(null, 1, 2) == 3 );
arguments
(function(a) {
assert( arguments[0] == 1 );
assert( arguments[1] == 2 );
})(1, 2);
(function(/* args */) {
assert( arguments.length == 2 );
assertThrows( arguments.indexOf(2) );
})(1, 2);
(function(/* args */) {
var args =
Array.prototype.slice.call(arguments);
assert( args.indexOf(2) == 1 );
})(1, 2);
var ids = [1, 2];
for (var i = 0; i < ids.length; i += 1) {
$.getJSON('/comments/'+ ids[i],
function(c) {
$('#comment-'+ ids[i]).html(c.body);
});
}
var ids = [1, 2];
ids.forEach(function(id) {
$.getJSON('/comments/'+ id, function(c){
$('#comment-'+ id).html(c.body);
});
});
var ids = [1, 2];
function display(id) {
$.getJSON('/comments/'+ id, function(c){
$('#comment-'+ id).html(c.body);
});
}
for (var i = 0; i < ids.length; i += 1) {
display(ids[i]);
}
var obj = {
foo: 1,
bar: 2,
sum: function(a, b) {
return a + b;
}
};
assert( obj.foo == 1 );
for (var prop in obj) {
assert( typeof prop == 'string' );
assert( prop in obj );
obj[prop];
}
assert( obj['foo'] == 1 );
var s = 'baz';
obj[s] = 3;
assert( obj.baz == 3 );
assert( typeof obj.whatev == 'undefined' );
delete obj.foo
assert( typeof obj.foo == 'undefined' );
assert( !('foo' in obj) );
var proto = { foo: 1 };
function Proto() {};
Proto.prototype = proto;
var obj = new Proto();
assert( obj.foo == 1 );
assert( !obj.hasOwnProperty('foo') );
proto.foo = 2;
assert( obj.foo == 2 );
var proto = { foo: 1 };
var obj = Object.create(proto);
assert( obj.foo == 1 );
assert( !obj.hasOwnProperty('foo') );
proto.foo = 2;
assert( obj.foo == 2 );
var Button = function(interval) {
var lastPushed = new Date();
this.push = function() {
if (intervalElapsed()) {
throw "You waited too long.";
} else {
lastPushed = new Date();
}
};
function intervalElapsed() {
var now = new Date();
return (now - lastPushed) < interval;
}
};
var button = new Button(108);
button.push();
var Button = Class.extend({
init: function(interval) {
this.interval = interval;
this.lastPushed = new Date();
},
push: function() {
if (this._intervalElapsed()) {
this._badThings();
} else {
this.lastPushed = new Date();
}
},
_intervalElapsed: function() {
var now = new Date();
return (now - this.lastPushed) < this.interval;
},
_badThings: function() {
throw "You waited too long";
}
});
var EventedButton = Button.extend({
init: function(interval) {
this._super(interval);
this._startCount();
},
push: function() {
clearTimeout(this.timeout);
this._startCount();
},
_startCount: function() {
var self = this;
this.timeout = setTimeout(function() {
self._badThings();
}, this.interval);
}
});
var button = new Button(108);
button._badThings();
setInterval(function() {
clearTimeout(button.timeout);
button._startCount();
}, 104);
var Button = jive.oo.Class.extend(function(protected) {
this.init = function(interval) {
this.interval = interval;
this.lastPushed = new Date();
};
this.push = function() {
if (this.intervalElapsed()) {
this.badThings();
} else {
this.lastPushed = new Date();
}
};
protected.intervalElapsed = function() {
var now = new Date();
return (now - this.lastPushed) < this.interval;
};
protected.badThings = function() {
throw "You waited too long";
};
});
var EventedButton = Button.extend(function(protected) {
this.init = function(interval) {
this._super(interval);
this.startCount();
};
this.push = function() {
clearTimeout(this.timeout);
this.startCount();
};
protected.startCount = function() {
var self = this;
this.timeout = setTimeout(function() {
self.badThings();
}, this.interval);
};
});
var button = new Button(108);
assert( typeof button.badThings == 'undefined' );
assert( typeof button.timeout == 'undefined' );
assert( typeof button.startCount == 'undefined' );
button.push();
var i = 0, array = [];
setInterval(function() {
i += 1;
array.push(i);
}, 1000);
var i = 0, array = [];
setInterval(function() {
i += 1;
array.push(i);
}, 1000);
var mark = new Date(), now;
while(true) {
now = new Date();
if (now - mark >= 1000) {
mark = now;
array.push('x');
}
}
var i = 0;
$.getJSON('/feed', function(feed) {
display(feed.count +" unread items");
i += 1;
assert( i == 2 );
});
i += 1;
assert( i == 1 );
var i = 0, array = [];
setInterval(function() {
i += 1;
array.push(i);
}, 1000);
setInterval(function() {
array.push('x');
}, 1000);
$('#clickme').click(function() {
$('#clickme').text('clicked!');
});
$('#clickme').text('not clicked');
From John Resig's blog, http://ejohn.org/blog/how-javascript-timers-work/
test('strict equality does not coerce', function() {
ok( 3 !== "3", '3 is not strictly equal to "3"' );
});
module('equality', {
setup: function() {
this.array = [1, 2, 3];
},
teardown: function() {
delete this.array;
}
});
test('array comparisons are by identity', function() {
ok( this.array != this.array.slice(),
'two arrays with the same values or not equal' );
});
asyncTest('fetches a feed', 1, function() {
$.getJSON('/feed', function(feed) {
ok( feed.count > 0,
'fetched feed with unread items' );
start();
});
});
module('feed API', {
setup: function() {
this.oldGet = $.getJSON
$.getJSON = function(url, callback) {
setTimeout(function() {
callback({ count: 3 });
}, 0);
};
},
teardown: function() {
$.getJSON = this.oldGet;
}
);
asyncTest('fetches a feed', 1, function() {
$.getJSON('/feed', function(feed) {
ok( feed.count > 0,
'fetched feed with unread items' );
start();
});
});
var myObj = {
foo: 1,
bar: 2,
}
Array#indexOf()
if ([1, 2, 3].indexOf('foo') > -1) {
return true;
}
$.inArray()
.return
and return valuefunction getVals() {
return
{
foo: 1, bar: 2
};
}
undefined
.$('#someDiv').css('width', 'NaNpx');
var oldWrite = document.write;
document.write = function() {
if (!$.isReady) {
return oldWrite.apply(document,
arguments);
}
};
Borrow functions from Function.prototype.
return Function.prototype.apply.call(oldWrite, document, arguments);
Use framework methods for checking type, such as $.isFunction().
max-age=0
headermax-age=0
header