arrow keys to switch slidesctrl+r to run the code samplesctrl+i to focus the code editoresc to blur the code editor or to show the slide overview
JS had to 'look like Java' only less so, be Java’s dumb kid brother or boy-hostage sidekick. Plus, I had to be done in ten days or something worse than JS would have happened
- Brendan Eich, 2010
+ adds and concatenatestypeofwith and evalundefined, NaN== and !=
var object = {};
object.name = 'Gob';
object['name'] = 'Gob';
console.log(object.name);
var name = object.name;
var name = object['name'];
console.log(name);
delete object.name;
delete object['name'];
console.log(object.name);
var emptyObject = { };
var myObject = {
'some property': 'some value',
foo: emptyObject,
method: function(param) {
// do something
}
};
Object.prototype
var Person = function (name) {
this.name = name;
};
Person.prototype.sayHello = function () {
console.log("Hello, I'm " + this.name);
};
var tobias = new Person('Tobias');
tobias.sayHello();
var Person = function (name) {
this.name = name;
};
Person.prototype.sayHello = function () {
console.log("Hello, I'm " + this.name);
};
var tobias = new Person('Tobias');
tobias.sayHello();
Person.prototype.sayMyName = function () {
console.log('My name is ' + this.name);
};
tobias.sayMyName();
var Person = function () { };
Person.prototype.sayHello = function () {
console.log("Hello, I'm " + this.name);
};
var Bluth = function (name) {
this.name = name;
};
Bluth.prototype = new Person();
var bluth = new Bluth('Buster');
bluth.sayHello();
console.log(parseInt('123', 10));
console.log(parseInt('010', 10));
console.log(parseInt('hello'));
true or false0, "", NaN, null and undefined are falsy &&, || and !
var array = [
'one',
'two',
{ number: 'three' },
function() {}
];
var date = new Date('November 15, 2012');
console.log(date.getFullYear());
console.log(date.getMonth());
console.log(date.getDate());
var str = 'me@example.com'; var pattern = /[\w-]+@[\w-]+\.+[\w-]+/i; console.log(str.match(pattern)); console.log(pattern.test(str));
function
var add = function(a, b) {
return a + b;
};
function add(a, b) {
return a + b;
}
These patterns differ in how this is initialized
this is bound to that object (late binding)
var myObject = {
value: 2,
increment: function(inc) {
this.value += inc;
console.log(this);
console.log(this.value);
}
};
myObject.increment(2);
this is bound to the global object
var foo = function() {
console.log(this);
};
foo();
var myObject = {
value: 1,
double: function() {
var helper = function() {
console.log(this.value);
};
helper(); // invoke as a function
}
};
myObject.double(); // invoke as a method
new prefix, a new object will be created with a hidden link to the value of the function's prototype memberthis is bound to that new object
var Foo = function(val) {
this.value = val;
console.log(this.value);
console.log(this);
};
var myFoo = new Foo('confused');
var myFoo = Foo('confused');
apply or call method lets us choose the value of this
var add = function(a, b) {
console.log(a + b + this.c);
};
var context = {
c: 5
};
add(3, 4);
add.apply(context, [3, 4]);
add.call(context, 3, 4);
this, all functions get a bonus parameter arguments
var sum = function(){
var i, sum = 0;
for(i = 0; i < arguments.length; i += 1){
sum += arguments[i];
}
return sum;
};
console.log(sum(3, 7, 9, 23));
var myVar = 'my value';
function bar() {
console.log(myVar);
var myVar = 'local value';
}
bar();
var myVar = 'my value';
function bar() {
var myVar = undefined;
console.log(myVar);
myVar = 'local value';
}
bar();
console.log(subtract(2, 3));
function subtract(a, b) {
return a - b;
}
var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
console.log(foo);
}
bar();
var foo = 1;
function bar() {
var foo = undefined;
if (!foo) { // truthy
foo = 10;
}
console.log(foo);
}
bar();
function bar() {
x = 10;
}
bar();
console.log(x);
function foo() {
var y = 10;
}
foo();
console.log(y);
function foo(x) {
var tmp = 3;
function bar(y) {
console.log(x + y + (++tmp));
}
bar(10);
}
foo(2);
foo(2);
foo(2);
foo(2);
function foo(x) {
var tmp = 3;
return function (y) {
console.log(x + y + (++tmp));
}
}
var bar = foo(2);
bar(10);
bar(10);
bar(10);
bar(10);
(function (root) {
var privateVariable;
function privateFunction(x) {
privateVariable = x;
}
root.firstMethod = function (y) {
privateFunction(y);
};
root.secondMethod = function () {
console.log(privateVariable);
};
}(this));
this.firstMethod(10);
this.secondMethod();
var singleton = (function () {
var privateVariable;
function privateFunction(x) {
privateVariable = x;
}
return {
firstMethod: function (y) {
privateFunction(y);
},
secondMethod: function () {
console.log(privateVariable);
}
};
}());
singleton.firstMethod(10);
singleton.secondMethod();
(function (window, undefined) {
var document = window.document,
$ = window.jQuery.noConflict();
var root = {};
// ...
window.root = root;
} (window));
var foo = function(){
return {
ok: true
};
};
console.log(foo().ok);
=== and !==, not == or !=if and whilenew prefix should start with a capital letter{} instead of new Object(). Use [] instead of new Array()eval is Evilwith
var trivia = Array(8).join('hi' + 1);
console.log(trivia);
Thomas Pedersen, Miles AS