On Github rcorral / presentation-js-for-beginners
Created by Rafael Corral May 2013
Everyone was a noob once, it's ok. We all sucked at one point in time. Teaching programming is difficult, everyone learns differently. If I talk too much and you want to see an example just stop me, because you won't be the only one. Or if I'm giving out too many examples and you need more explanation then let me know too.me@rafaelcorral.com@rcorrallinkedin.com/in/rafacorral Software Engineer at Barracuda Networks
null // Not NULL // or Null
undefined // Variable declared but not set // Not the same as null
true // and false
var months = 12; // int var height = 6.5; // float var year = months / 'x'; // NaNIntegers and floats
var first_name = "Rafael";Numbers, strings, and booleans are object-like in that they have methods, but they are immutable. Objects in JavaScript are mutable keyed collections. In JavaScript, arrays are objects, functions are objects, regular expressions are objects, and, of course, objects are objects.
var empty_object = {}; var user = { name: 'Rafael', age: 25 }Objects can appear anywhere where an expression can appear.
user['name']; // Rafael user.age; // 25 user.email; // undefined user.gender || 'none'; // none
var another_user = user; another_user.name = 'Travis'; user.name; // Travis
user.get_age = function() { return this.age; } var user_proto = function(){}; user_proto.prototype = user; var user2 = new user_proto; user2.age = 40; user2.get_age(); // 40 user.get_age(); // 25 user.get_name = function(){ return this.name; } user2.name = 'Rafael'; user.get_name(); // Travis
user.height = 72; user.hasOwnProperty('height'); // true user2.height; // 72 user2.hasOwnProperty('height'); // falseThis method is available in objects
for ( var i in user ) { if ( typeof user[i] !== 'function' ) { alert( i + ': ' + user[i] ); } }
The thing that's special about functions is that they can be invoked.
// Create a function that gets the remainder var remainder = function(a, b) { return a % b; }Functions are created with function literals. A function has 4 parts:
var person = function(fname, lname, age){ var get_name = function(){ return fname + ' ' + lname; }, get_age = function(){ return age; }; return { name: get_name(), age: get_age() } } var user_object = person('Rafael', 'Corral', 25);
When a function is invoked it get's the declared parameters. It receives two additional parameters: this and arguments. The value of this is determined by the invocation pattern.
var my_person = { age: 0, increment_age: function(inc){ this.age += typeof inc === 'number' ? inc : 1; return this; }, get_age: function(){ return this.age; } } my_person.increment_age().get_age(); // 1 my_person.increment_age(2).get_age(); // 3
var color = 'red'; var favorite_color = { get_color: function(){ this.color = 'blue'; function inner_function(){ return this.color; } return inner_function(); } } favorite_color.get_color(); // 'red'
var color = 'red'; var favorite_color = { get_color: function(){ this.color = 'blue'; var that = this; // Workaround function inner_function(){ return that.color; } return inner_function(); } } favorite_color.get_color(); // 'blue'
var Mammal = function( type ) { this.type = type; } // Create a public method Mammal.prototype.get_type = function(){ return this.type; } // Create a cat var cat = new Mammal('cat'); cat.get_type(); // 'cat'Functions that are intended to be used with the new prefix are called constructors. By convention, they are kept capitalized. If called without the new prefix, bad things can happen.this will get bound to the global object rather than the newely created one. It is not recommended to use this style of contructor functions.
// The first parameter is the value of this var array = [9, 6]; remainder.apply(null, array); // 3 cat.get_type.apply({type: 'dog'}); // 'dog'
var foo = function(){ var a = 3, b = 5; var bar = function(){ var b = 7, c = 11; // a is 3, b is 7, c is 11 a += b; // a is 10, b is 7, c is 11 } // a is 3, b is 5 bar(); // a is 10, b is 5 } foo();
var game = (function(){ // This value can't be modified var type = 'chess'; return { get_game: function(){ return type; } } }());
var mammal = function( type ) { return { get_type: function(){ return type; } } } // Create a cat var cat = mammal('cat'); cat.get_type(); // 'cat'type is private and will always be available to the inner method of the returned object.
var electronics = { type: '', get_type: function(){ return this.type; } }; var tv = function(inches){ var F = function(){}; F.prototype = electronics; _f = new F; _f.type = 'tv'; _f.inches = inches; _f.get_inches = function(){ return this.inches; } return _f; }; var samsung = tv(55); samsung.get_inches(); // 55 samsung.inches = 42; samsung.get_inches(); // 42
var electronics = function(type){ var that = {}; that.get_type = function(){ return type; } return that; }; var tv = function(inches){ var that = electronics('tv'); that.get_inches = function(){ return inches; } return that; }; var samsung = tv(55); samsung.get_inches(); // 55 samsung.inches = 42; samsung.get_inches(); // 55This pattern provides us with privacy. Once the inches and type are set they cannot be changed.
var empty = []; var numbers = [ 'zero', 'one', 'two', 'three', 'four', 'five' ]; empty[1]; // undefined numbers[1]; // 'one' numbers.legth; // 6 // object literal var numbers_object = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five' }
var some_array = []; some_array.length; // 0 some_array[100] = 'some value'; some_array.length; // 101 some_array.length = 0; some_array.length; // 0
.concat() .join() .pop() .push() .reverse() .shift() .unshift()
delete numbers[4]; // Leaves a hole in the array // Numbers is ['zero', 'one', 'two', 'three', undefined, 'five'] numbers.splice(4, 1); // numbers is ['zero', 'one', 'two', 'three', 'five']
Create a single global object for your application
var myapp = { options: { length: 1, width: 2, color: red }, function1: function(){ } };
Be cautious of the scope
var name = 'Rafael'; function dogs_name(){ name = 'Sunny'; } var my_dog = dogs_name(); name; // 'Sunny'
==
'' == '0' // false 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true ' \t\ r\ n ' == 0 // true // Using === would solve all this confusion
eval is evil
// Just don't do this eval('somecode');