This document provides a summary of an introductory presentation on advanced JavaScript concepts including closures, prototypes, inheritance, and more. The presentation covers object literals and arrays, functions as objects, constructors and the this keyword, prototypes and the prototype chain, classical and prototypal inheritance, scope, and closures. Examples are provided to demonstrate each concept.
Constructors when invokedwith new , functions return an object known as this you have a chance of modifying this before it's returned you can also return some other object
28.
Constructor functions var Person = function (name) { this .name = name; this .speaks = 'fr' ; this .say = function() { return "Je m'appelle " + this .name; }; };
29.
An object createdwith constructor >>> var julien = new Person( "Julien" ); >>> julien.say(); "Je m'appelle Julien"
30.
Constructor’s return valuevar Person = function (){ this .first = "Bruce" ; return {last: "Wayne" }; }; >>> typeof new Person().first "undefined" >>> new Person().last "Wayne"
31.
Constructor’s return valuevar Person = function (){ this .first = "Bruce" ; return "Batman" ; }; >>> new Person().first "Bruce"
constructor property>>> function Person(){}; >>> var jo = new Person(); >>> jo. constructor === Person true
34.
constructor property>>> var o = {}; >>> o. constructor === Object true >>> [1,2]. constructor === Array true
35.
Built-in constructor functionsObject Array Function RegExp Number String Boolean Date Error, SyntaxError, ReferenceError…
36.
var fn =new Function( 'a, b','return a+b'); var fn = function(a, b){ return a + b; } var re = new RegExp( '[a-z]', 'gmi'); var re = /[a-z]/gmi; var a = new Array(); var a = []; var o = new Object(); var o = {}; Not that Use this
37.
Wrapper objects vs.primitive >>> typeof new Number ( 1 ) "object" >>> typeof 1 "number"
How is theprototype used? when a function is invoked as a constructor var Person = function (name) { this .name = name; }; Person. prototype .say = function () { return this .name; }
44.
>>> var dude = new Person( 'dude' ); >>> dude.name; "dude" >>> dude.say(); "dude" How is the prototype used?
45.
say() isa property of the prototype object but it behaves as if it's a property of the dude object can we tell the difference? How is the prototype used?
Inheritance via theprototype >>> var Dad = function (){ this .family = "Stefanov" ;}; >>> var Kid = function (){}; >>> Kid. prototype = new Dad(); >>> var billy = new Kid(); >>> billy.family "Stefanov"
54.
Inherit one moretime >>> var GrandKid = function (){}; >>> GrandKid. prototype = billy; >>> var jill = new GrandKid(); >>> jill.family "Stefanov"
Classes? There areno classes in JavaScript Objects inherit from objects class ical inheritance is when we think of constructors as if they were classes
62.
Classical inheritance function Parent(){ this .name = 'parent' ;} Parent. prototype .getName = function (){ return this .name; }; function Child(){} inherit(Child, Parent);
Option 3 function inherit(C, P) { var F = function (){}; F. prototype = P. prototype ; C. prototype = new F(); }
66.
Option 3 +super function inherit(C, P) { var F = function (){}; F. prototype = P. prototype ; C. prototype = new F(); C.uber = P. prototype ; }
67.
Option 3 +super + constructor reset function inherit(C, P) { var F = function (){}; F. prototype = P. prototype ; C. prototype = new F(); C.uber = P. prototype ; // super C. prototype . constructor = C; // reset }
68.
Inheritance by copyingproperties After all, inheritance is all about code reuse function extend(parent) { var i, child = {}; for (i in parent) { child[i] = parent[i]; } return child; }
69.
Inheritance by copying…>>> var parent = {a: 1 }; >>> var child = extend(parent); >>> child.a 1
70.
Inheritance by copying…This was a shallow copy you can make a deep copy using recursion mixins / multiple inheritance
71.
Prototypal inheritance assuggested by Douglas Crockford no class-like constructors involved objects inherit from objects via the prototype
Global namespace everyvariable is global unless it's in a function and is declared with var global namespace should be kept clean to avoid naming collisions function scope can help
Closure #4 –corrected function make() { var i, a = []; for (i = 0 ; i < 3 ; i++) { a[i] = ( function (local){ return function (){ return local;} })(i) } return a; }
91.
Getter/Setter var getValue, setValue; ( function () { var secret = 0 ; getValue = function (){ return secret; }; setValue = function (v){ secret = v; }; })() // usage >>> getValue() 0 >>> setValue( 123 ) >>> getValue() 123
92.
Iterator function setup(x) { var i = 0 ; return function (){ return x[i++]; }; }
93.
Iterator usage >>> var next = setup([ 'a' , 'b' , 'c' ]); >>> next() 'a' >>> next() 'b'
94.
Loop through DOMelements - wrong // all elements will alert 5 for (var i = 1; i < 5; i++ ){ document.getElementById('btn'+i).onclick = function(){ alert(i); }; }
95.
Loop through DOMelements - correct // first element alerts 1, second 2,... for (var i = 1; i < 5; i++ ){ document.getElementById('btn'+i).onclick = ( function( i ){ return function(){ alert(i); }; } )(i) }
>>> typeof variable typeof is an operator, not a function Not typeof(variable) even if it works Returns a string, one of: "string", "number", "boolean", "undefined", "object", "function"
98.
typeof if ( typeof whatever === "undefined") { // whatever is not defined } if (whatever == undefined) { // hmm, not so sure }
99.
>>> obj instanceof MyConstructor Not instanceof() Returns true | false true for all constructors up the chain
obj. propertyIsEnumerable ("prop" ) Will it show up in a for-in loop Caution: enumerable properties of the prototype will return false but still show up in the for-in loop