Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript: The Language Andrew Miadowicz Program Manager DEV307.

Similar presentations


Presentation on theme: "JavaScript: The Language Andrew Miadowicz Program Manager DEV307."— Presentation transcript:

1

2 JavaScript: The Language Andrew Miadowicz Program Manager Microsoft andrew.miadowicz@microsoft.com @jmiadowicz DEV307

3 JavaScript

4 ECMAScript is just a funny name for JavaScript

5

6

7 C-style syntax but not a whole lot of it function everything() { var x = {a:10}, y = [1,2], z = function() { }; for(var p in x) { lbl: for(var i = 0; i < 10; i++) { switch(x[p]) { case 0: continue; default: break lbl; } if(x instanceof String || 'a' in x) delete x.a; } while(typeof true) { if(void false) { this.x = 3 / (-this.f()) ? /foo/g : new String("hello") } else { do { try { throw 3; } catch(e) { debugger; return; } finally {}} while(false); }

8

9 Objects are bags of properties var o1 = {}; o1.x = 3; o1.x = 4; var o2 = { x : 3 }; var o3 = { first: o2, second: function() { return 5; } } o2.x === 3; o2["x"] === 3; o2["the answer!"] = 42;

10 Objects have prototypes var o1 = { }; o1.valueOf() === "[object Object]" var o2 = {valueOf: function() { return 10; }}; o2.valueOf() === 10

11 Objects inherit from other objects var point2d = { x:10, y:20 }; var point3d = Object.create(point2d); point3d.z = 30; var location = Object.create(point2d); location.name = "Mandalay Bay"

12 // Data Property var bankAccount = { balance: 1257 } // Accessor Property var bankAccount2 = { get balance() { return 1257; } set balance(v) { }; }

13 Objects have a descriptor associated with each property Data Descriptor: { value : 47, writable : false, enumerable : true, configurable : true } Accessor Descriptor: { get : function location() { }, set : function location() { }, enumerable : true, configurable : true }

14 Objects new ES5 object reflection APIs var desc = Object.getOwnPropertyDescriptor(obj, name) Object.defineProperty(obj, name, desc) var names = Object.getOwnPropertyNames(obj) var protoObj = Object.getPrototypeOf(obj)

15 demo Accessor Properties Object.getOwnPropertyDescriptor Object.defineProperty Objects, Properties and the DOM

16 Objects key ideas

17 Functions are first-class values function sum(x, y) { return x + y; } var sum2 = function(x, y) { return x + y; } var sum3 = sum; function wrap(f) { return f(); } wrap(function() { return 5; })

18 Functions are objects function sum(x, y) { return x + y; } sum.length === 2; Object.getPrototypeOf(sum) === Function.prototype;

19 Functions have special rules for ‘this’ var obj = { sum: function(x,y) { return x + y; } } obj.sum(3,4); var obj2 = { offset: 7; sum: function(x,y) { return x + y + this.offset; } } obj2.sum(3,4) === 14; var f = obj2.sum; f(3,4) === NaN; f.call(obj2, 3, 4); f.apply(obj2, [3, 4]);

20 Functions have special behaviour when invoked with ‘new’ function Person(firstName,lastName) { this.firstName = firstName; this.lastName = lastName; } var bob = new Person("Bob", "Crites"); var anil = new Person("Anil", "Madhavan"); bob.firstName === "Bob"; Person.prototype.getFullName = function() { return this.firstName + " " + this.lastName; } bob.getFullName() === "Bob Crites"

21 Functions objects and prototypes and constructors, oh my! bob.firstName === "Bob"; bob.getFullName() === "Bob Crites"; anil.getFullName === bob.getFullName;

22 Functions objects and prototypes and constructors, oh my! bob instanceof Person Person.prototype.getFullName = function() {... }; new (bob.constructor)("Cynthia", "Washington")

23 Functions objects and prototypes and constructors, oh my! bob.valueOf() Person.valueOf()

24 Functions objects and prototypes and constructors, oh my!

25 demo Constructors and ‘new’ HTML element inheritance Constructors and the DOM

26 Functions have their own variable scopes // Scope (function() { var x = 5; x === 5; })(); x === undefined // Outer variable access var outer = "hello"; (function() { outer = "goodbye"; })(); outer === "goodbye"; // Isolating from changes in outer variables var outer = "hello"; (function(outer) { setInterval(function() {alert(outer);}, 100); })(outer); outer = "goodbye";

27 Functions important points

28 Strict Mode lets you opt-in to a more constrained subset of JavaScript (function () { "use strict"; // this function is strict... x = 5; // Error! arguments.caller; // Error! arguments.callee; // Error! var o = { x: 5}; Object.freeze(o); o.x = 7; // Throws }());

29 demo Undeclared variables Writing to non-writable properties Eval introducing variables to outer scope Strict Mode

30

31 Questions? andrew.miadowicz@microsoft.com

32 Connect. Share. Discuss. http://europe.msteched.com Learning Microsoft Certification & Training Resources www.microsoft.com/learning TechNet Resources for IT Professionals http://microsoft.com/technet Resources for Developers http://microsoft.com/msdn

33 Evaluations http://europe.msteched.com/sessions Submit your evals online

34 Questions? andrew.miadowicz@microsoft.com

35


Download ppt "JavaScript: The Language Andrew Miadowicz Program Manager DEV307."

Similar presentations


Ads by Google