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 person = { name: "Joe", age: 28 };
person.name = "Bad";
person["name"] = "Ugly";
var clients = [
'posadas', 'telcel', 'palacio'
];
result = clients.length; // -> 3
result = typeof(something) === "undefined";
"Hello World"
foo = 3.1459 * 2
mux = foo == bar ? baz : qux
if(cond) { expr } else { expr2 }
while(cond) { expr }
for(init; cond; incr) { expr }
for(var idx in object) { expr }
// Syntax error !
// Tried to use statement as expression.
result = if(1 > 2) { "hm" } else { "uh?" }
// statement
function foo() {}
CORE.out( typeof foo );
// expression
var foo = function() {}
CORE.out( typeof foo );
var foo = function bar() {}
CORE.out( typeof foo ); // function
CORE.out( typeof bar ); // undefined
// Assign anonymous functions to
// scoped variables.
var foo = function() { }
my_link.bind('click', function(e) {
e.preventDefault();
alert('You cliked the link');
};
setTimeout(function() {
alert('Timeout triggered');
}, 100);
(function(msg){
alert("hello "+msg);
})("world");
var fun = function(a, b) {
CORE.out( a + b );
}
fun();
fun("hey");
fun("whats", "up");
fun(14, 10);
fun("weak", 10);
var fun = function() { // no parameter names
CORE.out(arguments.length);
var name = arguments[0];
if("function" === typeof(arguments[1])) {
CORE.out("second argument is a function");
}
}
fun();
fun("hey", function() { foo }, "this");
var fun = function() {
CORE.out( arguments.callee.toString() );
}
fun();
var fun = function(arg) {
// the function being executed
var self = arguments.callee;
// this can be useful for memoizing data
var cache = self.cache || {}
, value = self.cache[arg] || exp(arg);
self.cache[arg] = value;
return cached;
}
fun("something");
window.name = 'John';
var get_name = function() {
return this.name;
};
result = get_name();
var person = {
name: 'John Lennon',
get_name: function() {
return this.name;
}
};
result = person.get_name();
var Person = function() {
this.name = 'John';
};
var someone = new Person();
var someone_else = new Person();
CORE.out(someone === someone_else);
CORE.out(someone.name === someone_else.name);
var person = {
greet: function() {
return 'Hello ' + (this.name || '');
}
};
CORE.out( person.greet() );
CORE.out( person.greet.apply({name: 'John'}) );
var add = function() {
var i, sum = this.memory || 0;
for (i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return (this.memory = sum);
};
CORE.out( add(1, 2, 3) );
CORE.out( add.apply({memory: 10}, [1, 2, 3]) );
CORE.out( add.call({memory: 21}, 1, 2, 3, 4) );
var fun = function() {
var baz = "Hello World";
CORE.out(baz);
}
fun();
CORE.out( typeof baz );
var baz = "Hello World";
var fun = function() { CORE.out(baz); }
fun();
baz = "Goodbye World";
fun();
var foo = function() {
var a = 'test';
if ( a === 'test' ) {
var b = true;
}
CORE.out(b);
}();
CORE.out(foo);
var mult = function(a) {
return function(b) {
return a * b;
}
}
var twice = mult(2);
CORE.out( twice(12) );
CORE.out( mult(12)(12) );
var obtainAPI = function(url) {
var thisIsPrivate = someOperation(url);
var foo = funcion(a, callback) {
var res = foo(thisIsPrivate + a);
callback(res);
}
return {
exposedMethod: doBlah
}
}
var api = optainAPI("jwmsolutions.com");
api.exposedMethod("cool", function(resp) {
console.debug(resp);
});
var mySingleton = (function() {
var myPrivateVar = 'hello world';
// more private vars
var myPrivateMethod = function() {};
// more private methods
return {
doSomething: function() {
return myPrivateVar.toUpperCase();
},
doSomethingElse: function() {
myPrivateMethod();
}
};
})();
CORE.out(mySingleton.doSomething());
CORE.out(mySingleton.myPrivateVar);
Number.prototype.integer = function() {
return Math[this < 0 ? 'ceil' : 'floor'](this);
};
var a = 10, b = 10.21, c = -12.84;
CORE.out(a.integer());
CORE.out(b.integer());
CORE.out(c.integer());
Function.prototype.andThen = function(g) {
var f = this;
return function() {
f();
g();
};
};
var sayHi = function() {
CORE.out('Hello, world!');
};
var sayBye = function() {
CORE.out('Goodbye!');
};
sayHi.andThen(sayBye)();
Function.prototype.andThen = function(g) {
var f = this; return function() { f(); g(); };
};
var Manager = function() {
this.callback = function() {};
this.registerCallback = function(f) {
this.callback = (this.callback).andThen(f);
};
};
var sayHi = function() { CORE.out('Hello, world!'); };
var sayBye = function() { CORE.out('Goodbye!'); };
var manager = new Manager();
manager.registerCallback(sayHi);
manager.registerCallback(sayBye);
manager.registerCallback(sayHi);
manager.callback();
var Cat = function(name) {
this.name = name;
};
var myCat = new Cat("micha");
CORE.out( myCat.name );
var Cat = function(name) {
this.name = name;
};
Cat.prototype.get_name = function() {
CORE.out( this.name );
};
Cat.prototype.say_meow = function() {
CORE.out( 'meow' );
};
var myCat = new Cat("micha");
myCat.get_name();
myCat.say_meow();
var Cat = function(name) { this.name = name; };
Cat.prototype.say_meow = function() {
CORE.out('meow');
};
var Kitten = function(name) {
this.name = name;
};
Kitten.prototype = new Cat();
Kitten.prototype.purr = function() {
CORE.out('r-r-r-r-r');
};
var myKitten = new Kitten("catrin");
CORE.out(myKitten.name);
myKitten.say_meow();
myKitten.purr();
Object.create = function(o) {
var F = function() {};
F.prototype = o;
return new F();
};
var cat = {
name: "",
get_name: function() {
CORE.out(this.name);
},
say_meow: function() {
CORE.out('meow');
}
};
var myCat = Object.create(cat);
myCat.name = "micha";
myCat.get_name();
myCat.say_meow();
var cat = {
name: "",
get_name: function() {
CORE.out(this.name);
}
};
var kitten = Object.create(cat);
kitten.name = "catrin";
kitten.say_meow = function() {
CORE.out('meow');
};
kitten.get_name();
kitten.say_meow();
var cat = function(name) {
var meow = 'meow';
var do_meow = function() {
CORE.out(meow);
};
return {
get_name: function() {
CORE.out(name);
},
say_meow: function() {
do_meow();
}
};
};
var myCat = cat("micha");
CORE.out(typeof myCat.name);
myCat.get_name();
myCat.say_meow();
CORE.out(typeof myCat.do_meow);
var cat = function(spec) {
var that = {};
var do_meow = function() {
CORE.out('meow');
};
that.get_name = function() {
CORE.out(spec.name);
};
that.say_meow = function() {
do_meow();
};
return that;
};
var myCat = cat({name: 'micha'});
CORE.out(typeof myCat.name);
myCat.get_name();
myCat.say_meow();
CORE.out(typeof myCat.do_meow);
var cat = function(spec) {
return {
get_name: function() {
CORE.out(spec.name);
}
};
};
var kitten = function(spec) {
var that = cat(spec);
var do_purr = function() {
CORE.out('r-r-r-r-r');
};
that.purr = function() {
do_purr();
};
return that;
};
var myKitten = kitten({name: 'catrin'});
myKitten.get_name();
myKitten.purr();
CORE.out( '' == '0' );
CORE.out( 0 == '' );
CORE.out( 0 == '0' );
CORE.out( false == 'false' );
CORE.out( false == '0' );
CORE.out( false == undefined );
CORE.out( false == null );
CORE.out( null == undefined );
CORE.out( ' \t\r\n ' == 0 );
var output = function() {
return
{
status: true
}
}();
CORE.out( output );
CORE.out( output && output.status );
abstract boolean break byte
case catch char class const continue
debugger default delete do double
else enum export extends
false final finally float for function goto
if implements import in instanceof int
interface long native new null
package private protected public return
short static super switch synchronized
this throw throws transient true try typeof
var volatile void while with
CORE.out( typeof null );
CORE.out( typeof NaN );
CORE.out( NaN === NaN );
CORE.out( isNaN(NaN) );
// object-oriented way
_(['John', 'Paul', 'George', 'Ringo']).each(function(beatle) {
console.log(beatle);
});
// functional way
_.each(['John', 'Paul', 'George', 'Ringo'], function(beatle) {
console.log(beatle);
});
_.reduce([1.5,2,3.7], 0, function(memo, num) { return memo + Math.floor(num) });
// => 6
var beatles = [
{name: 'John', dead: true},
{name: 'Paul', dead: false},
{name: 'Ringo', dead: false},
{name: 'George', dead: true}
]
_.pluck(beatles, 'name');