http://github.com/jsantell/fundamentals-of-javascript-slides
var x = 5;
var x = 5; x = 5 * 4;
var x = 5; var y = 8;
var x = 5; var y = 8; x = x + 3 + y; y = x * 2;
var x = 5; var y = 8; x = x + 3 + y; y = x * 2;
var name = 'Tom';
var isOn = true;
Use var when defining!
Will explain scoping later
var x = 5 * 6 + 1 / 4 - 5 x // 25.25
var x = 14 % 12 x // 2
var x = 5; x++; // 5 x // 6
var x = 5; ++x; // 6 x // 6
7 & 9; // 1 // 0111 // & 1001 // ---- // 0001
var x = 5; x *= 2; // x = x * 2; x; // 10
var a = ['abc', 4, false];
var x = ['a', 'b', 'c']; x[0]; // 'a' x[2]; // 'c'
var x = ['a', 'b', 'c']; x[0]; // 'a' x[0] = 'changed!'; x[0]; // 'changed!'
var x = ['a', 'b', 'c']; x.length; // 3 x[10] = 'j'; x.length // 11
function (val) {
val = [].concat(val);
// iterate over all
// elements of `val`
}
var user = 'Beth';
if (user === 'Beth')
console.log("Yup, it's Beth");
var user = 'Beth';
if (user === 'Beth')
console.log("Yup, it's Beth");
else
console.log("Nope, not Beth");
var user = 'Beth';
if (user === 'Beth')
console.log("Yup, it's Beth");
else if (user === 'Joe')
console.log("Not Joe either");
else
console.log("Who is this?");
var user = 'Beth';
if (user === 'Beth') {
console.log("Yup, it's Beth");
loginUser(user);
}
else
console.log("Nope, not Beth");
var msg = loggedIn() ? 'profile' : 'sign in';
var w = true && true; var x = true && false; var y = false && false; var z = false && true;
var w = true || true; var x = true || false; var y = false || false; var z = false || true;
var name = userName || 'Unknown';
var n = 0;
while (n < 10) {
console.log(n + ' sheep');
n++;
}
var u = ['joe', 'sue', 'tom'];
for (var i = 0; i < u.length; i++) {
console.log('Here is ' + u[i]);
}
// "Here is joe"
// "Here is sue"
// "Here is tom"
<html>
<head></head>
<body>
<script src="script.js"></script>
</body>
</html>
typeof 5; // 'number' typeof 'hello'; // 'string' typeof false; // 'boolean' typeof []; // 'object'
var n = 5;
var s = 'hello';
var b = true;
var a = [];
var o = {};
typeof 5; // 'number'
typeof (new Number(5)); // 'object'
typeof 'hi'; // 'string'
typeof (new String('hi')); // 'object'
5 === 5; // true new Number(5) === new Number(5); // false
typeof null; // 'object' typeof undefined; // 'undefined' null == undefined; // true null === undefined; // false
var x = 'hello';
if (x) console.log('truthy!');
var a = [];
if (0)
a.push('hello');
if (a)
console.log('the array is truthy!');
var a = [];
if (a.length)
console.log('hello!');
// What happens? 5 === '5' 5 == '5' null == undefined null === undefined
// What happens? 5 === '5'; // false 5 == '5'; // true null == undefined; // true null === undefined; // false
var array1 = ['hi']; var array2 = ['hi']; var array3 = array1; array1 !== array2; array1 === array3;
Non primitives are objects
Hash, map, dictionary, key-value
var o = {};
o.name = 'Jordan';
o.lang = 'JavaScript';
o.name; // 'Jordan'
o.hairColor; // undefined
var o = {
name: 'Jordan',
lang: 'JavaScript'
};
o.name; // 'Jordan'
o.hairColor; // undefined
var o = {};
o['name'] = 'Jordan';
o['lang'] = 'JavaScript';
o.name; // 'Jordan'
o.hairColor; // undefined
var o = {};
o['π'] = Math.PI;
o['½'] = 0.5;
o[''] = null;
var o = {
name: 'Jordan',
lang: 'JavaScript'
};
Object.keys(o);
// ['name', 'lang']
var o = {
name: 'Jordan',
lang: 'JavaScript'
};
var keys = Object.keys(o);
for (var i = 0; i < keys.length; i++)
console.log(keys+': '+o[keys[i]];
// 'name: Jordan'
// 'lang: JavaScript'
An array of objects, each object representing a person with 'name', and 'age' properties.
Print out all properties of all objects one at a time, if the age is greater than some age.
Procedure that takes arguments and has a context
square(5); // 25
square(3); // 9
function square (x) {
return x * x;
}
square(5); // 25
square(3); // 9
function square (x) {
return x * x;
}
square(5); // 25
square(3); // 9
var square = function (x) {
return x * x;
}
square(5); // 25
square(3); // 9
// `square` IS NOT DEFINED!
var square = function (x) {
return x * x;
}
var square = function (x) {
return x * x;
}
square(5); // 25
square(3); // 9
console.log(y); // undefined var y = 5; console.log(y); // 5
var x = 5;
console.log(x); // 5
console.log(y); // throws
function fn () {
var y = 1;
console.log(x); // 5
console.log(y); // 1
}
var incrementer = createIncrement(5);
incrementer(); // 6
incrementer(); // 7
incrementer(); // 8
function createIncrement (x) {
var inc = x;
return function () {
return ++inc;
}
}
var doSomething = function () {}
[1,2,3].forEach(doSomething);
[1,2,3].forEach(function (x) {
console.log(x);
});
forEach([1,2,3], function (x) {
console.log(x);
});
[1,2,3].map(function (x) {
return x * x;
});
// [1,4,9];
map([1,2,3], function (x) {
return x * x;
});
// [1,4,9];
[1,2,3].reduce(function (sum, x) {
return sum + x;
}, 0);
// 6
var id = 12345;
getUserInfo(id, function (user) {
console.log(user);
});
console.log(1);
getUserInfo(id, function (user) {
console.log(2);
});
console.log(3);
// 1
// 3
// 2
function fakeAjax (id, callback) {
setTimeout(function () {
callback({});
}, 2000);
}
var person = new Person();
var person = new Person();
function Person (options) {
options = options || {};
this.name = options.name;
this.lang = options.lang;
}
var person = new Person({
name: 'Brendan Eich'
});
person.name; // 'Brendan Eich'
function Person (options) {
options = options || {};
this.name = options.name;
this.lang = options.lang;
}
function Person (options) {
options = options || {};
this.name = options.name;
}
Person.prototype.walk = function () {
console.log(this.name + ' walks!');
this.walking = true;
}
var per = new Person(); var dev = new Developer(); per instanceof Person; // true dev instanceof Person; // true per instanceof Developer; // false dev instanceof Developer; // true per instanceof Object; // true
var Person = {
age: 0,
walk: function () {}
};
var person = Object.create(Person, {
name: 'Carrie Mathison',
age: 34
});
function extend (source, obj) {
for (var prop in obj)
source[prop] = obj[prop];
return source;
}
var JugglerMixin = {
juggle: function () {}
};
extend(Person.prototype, JugglerMixin);
extend(Animal.prototype, JugglerMixin);
Object.defineProperty(p, 'full', {
get: function () {
return this.first + ' ' + this.last;
},
set: function (name) {
this.first = name.split(' ')[0];
this.last = name.split(' ')[1];
}
});
function handleLogin (event) {
loginUser(this);
}
function Person () {
this.onLogin = handleLogin.bind(this);
}
function add (x, y) {
return x + y;
}
function delay (fn) {
return function () {
var args = arguments;
setTimeout(function () {
fn.call(null, args[0], args[1]);
}, 1000);
}
}
var delayedAdd = delay(add); delayedAdd(1, 3); // 4
function delay (fn) {
return function () {
var args = arguments;
setTimeout(function () {
fn.apply(null, args);
}, 1000);
}
}
var delayedAdd = delay(add); delayedAdd(1,2,3,4); // 10