1.15k likes | 1.5k Views
Advanced JavaScript: closures, prototypes, inheritance. Stoyan Stefanov Ajax Experience, Boston 2008. About the presenter. Yahoo! performance team member YSlow 2.0 architect, dev Book author, open-source contributor Blog: http://phpied.com.
E N D
Advanced JavaScript:closures, prototypes, inheritance Stoyan Stefanov Ajax Experience, Boston 2008
About the presenter Yahoo! performance team member YSlow 2.0 architect, dev Book author, open-source contributor Blog: http://phpied.com
Firebug console… Inspect the contents of objects by clicking on them Tab auto-complete, a.k.a cheatsheet Arrows ↑ and↓ Fiddle with any page
JavaScript data types primitive and objects number string boolean undefined null
What’s an object? a hash of key => value pairs if a key (property) happens to be a function, we can call it a method
What’s an object? var obj = { shiny: true, isShiny: function() { returnthis.shiny; } }; obj.isShiny(); // true
Object literal notation { Wrapped in curly braces } ,-delimited properties key:value pairs var obj = {a: 1, "b c d": 2};
Arrays arrays are also objects auto-incremented properties
Arrays >>> var a = [1,3,2]; >>> a[0] 1 >>> typeof a "object"
Arrays array objects also get some cool properties... >>> a.length 3 ...and methods >>> a.sort() >>> a.join(' < ') "1 < 2 < 3"
Array literal notation var array = [ "Square", "brackets", "wrap", "the", "comma-delimited", "elements" ];
JSON JavaScript Object Notation Uses object and array literals Quotes required for properties {"num": 1, "str": "abc", "arr": [1,2,3]}
Functions functions are objects they have properties they have methods can de copied, deleted, augmented... special feature: invokable
Functions function boo(what) { return what; } or var boo = function(what) { return what; };
Functions function boo(what) { return what; } or var boo = functionbootoo(what) { return what; };
Functions are objects >>> boo.length 1 >>> boo.name "bootoo"
Functions are objects >>> var foo = boo; >>> foo("doodles") "doodles" >>> foo.call(null, "moo!"); "moo!"
Return value all functions return a value if they don't explicitly, they return undefined implicitly functions can return other functions
Constructors when invoked with 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
Constructor functions var Person = function(name) { this.name = name; this.speaks = 'fr'; this.say = function() { return "Je m'appelle " + this.name; }; };
An object created with constructor >>> var julien = new Person("Julien"); >>> julien.say(); "Je m'appelle Julien"
Constructor’s return value var Person = function(){ this.first = "Bruce"; return {last: "Wayne"}; }; >>> typeof new Person().first "undefined" >>> new Person().last "Wayne"
Constructor’s return value var Person = function(){ this.first = "Bruce"; return"Batman"; }; >>> new Person().first "Bruce"
Naming convention MyConstructor myFunction
constructor property >>> function Person(){}; >>> var jo = new Person(); >>> jo.constructor === Person true
constructor property >>> var o = {}; >>> o.constructor === Object true >>> [1,2].constructor === Array true
Built-in constructor functions Object Array Function RegExp Number String Boolean Date Error, SyntaxError, ReferenceError…
Wrapper objects vs. primitive >>> typeof new Number(1) "object" >>> typeof1 "number"
Primitives can act as objects >>> "test".length 4 >>> (123.456).toFixed(2) "123.46"
prototype a property of the function objects >>> var boo = function(){}; >>> typeof boo.prototype "object"
Prototypes can be augmented >>> boo.prototype.a = 1; >>> boo.prototype.sayAh = function(){};
Prototypes can be overwritten >>> boo.prototype = {a: 1, b: 2};
How is the prototype used? when a function is invoked as a constructor var Person = function(name) { this.name = name; }; Person.prototype.say = function() { return this.name; }
>>> var dude = new Person('dude'); >>> dude.name; "dude" >>> dude.say(); "dude" How is the prototype used?
say() is a 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?
Own properties vs. prototype’s >>> dude.hasOwnProperty('name'); true >>> dude.hasOwnProperty('say'); false
isPrototypeOf() >>> Person.prototype.isPrototypeOf(dude); true >>> Object.prototype.isPrototypeOf(dude); true
__proto__ I, the dude, have a secret link to the prototype of the constructor that created me __proto__ is not directly exposed in all browsers
>>> dude.__proto__.hasOwnProperty('say') true >>> dude.prototype ??? // Trick question >>> dude.__proto__.__proto__.hasOwnProperty('toString') true __proto__