Am I? Shawn Wildermuth – [email protected] Wilder Minds LLC – http://wilderminds.com 10 Time Microsoft MVP (Mostly that means I’m old) Book Author – Pragmatic ADO.NET – MS Press Training Kits – Silverlight Appendix for WPF Programming (O’Reilly) – Essential Windows Phone (7.5 and 8 coming soon)
System; namespace Example { public class Dog : Pet, IAnimal { public Dog() : base(4) { } FoodType _foodType = FoodType.Dry; public FoodType FoodType { get { return _foodType; } set { _foodType = value; } } public override void Feed(FoodType food) { base.Feed(food); } } } Requiring Libraries Defining Packaging Inheritance/Implementation Construction State Exposing State Behavior Defining Scope
and Loose Typing .NET and JavaScript are Different // C# var customer = new Customer(); // JavaScript var customer = new Customer(); Compiler Inferred Variable Declaration
and Loose Typing Strong Typing –Types are defined by names and static structure –Compiler does the checks for you –Compiler can infer the type when necessary // C# var x = 0; // Infers type to be int bool isInt = x is int; // true x = new object(); // Compilation Error
and Loose Typing Loose Typing –Types are typically defined by structure not by identity –Runtime checks –Type is dynamic // JavaScript var x = 0; // creates variable x that holds a number var isNumber = typeof x == "number"; // Works but limited x = new Object(); // no problem, redefines x with new type
and Loose Typing Strong Typing and OOP –Inheritance and interfaces are part of type identity // C# class Dog : Animal, IMoveable {...} class Car : Vehicle, IMoveable {...} // Accepts Any Animal (base class has Feed Method) void Feed(Animal ani) { ani.Feed(); } // Accepts any object that implements Interface void Move(IMoveable object) { object.Move(); }
and Loose Typing Loose Typing and OOP –Type is less important, but shape is important –Classic Duck Typing // JavaScript // Accepts any object // (must implement Feed function) function Feed(ani) { ani.Feed(); } // Accepts any object // (must implement Move method) function Move(object) { object.Move(); }
Typing Dynamic Typing Can be Powerful –"With much power comes much responsibility" // JavaScript var x = { name: "Shawn", city: "Atlanta" }; x.phone = "404-555-1212"; x.makeCall = function () { callSomeone(this.phone); };
Parameters Looks like C#, but not… // JavaScript function foo(one, two, three) { alert(one); alert(two); alert(three); } foo(1); // two and three are undefined // JavaScript function foo(one, two, three) { alert(one); if (two) alert(two); if (three) alert(three); } foo(1); // two and three are undefined
is 'this'? Function Body Variable –"this" applies to the owner of the function // JavaScript var f = function() { alert(this); }; f(); // [Object Window] (huh?)
is 'this'? 'this' is Owner –bind() lets you change the owner // JavaScript var obj = { name: "myObj", myFunc: function() { log(this); } }; obj.myFunc(); // this == obj var f = obj.myFunc.bind(this); // Copy Function with global f(); // this == global object
References outside are accessible in function –Regardless of lifetime // JavaScript var x = 1; function someFunction() { // Works as it wraps 'x' with a closure var y = x; } // Much Later someFunction();
C# is different than JavaScript // C# var a = "Hello"; if (true) { // This works var b = a; } // This doesn't work var c = b; // JavaScript var a = "Hello"; if (true) { // This works var b = a; } // This works too var c = b; // JavaScript var a = "Hello"; function () { // This works (closure) var b = a; } // This doesn't (functions define scope) var c = b;
in JavaScript In .NET, Class is Standard Unit of Work –Containers for data, code and behavior // C# class Customer { string Name { get; private set; } string Company { get; private set; } Customer(string name, string company = "") { Name = name; Company = company; } }
in JavaScript No such thing as a "Class" in JavaScript –But you can mimic them with some effort // JavaScript function Customer(name, company) { this.name = name; this.company = company; } var cust = new Customer("Shawn", "Wilder Minds"); var name = cust.name;
in JavaScript Member Functions Work Fine // JavaScript function Customer(name, company) { this.name = name; this.company = company; this.sendEmail = function (email) { ... }; } var cust = new Customer("Shawn", "Wilder Minds"); cust.sendEmail("[email protected]");
JavaScript "Classes" Sharing a Function –That way each instance doesn't have it's own copy // JavaScript function Customer(name, company) { this.name = name; this.company = company; } // Works but no access to private/member data Customer.send = function (email) { ... }; // JavaScript function Customer(name, company) { this.name = name; this.company = company; } // Gives access to each instance of Customer Customer.prototype.send = function (email) { ... }; var cust = new Customer("Shawn"); cust.send("[email protected]"); // WORKS
in JavaScript "Classes" Basic Inheritance with the Prototype object –The basic is-a relationship // JavaScript function Animal(foodType) { this.foodType = foodType; } Animal.prototype.feed = function () { alert("Fed the animal: " + this.foodType); }; var a = new Animal("None"); a.feed(); // "None" var test = a instanceof Animal; // true // JavaScript function Cow(color) { this.color = color; } // Inheritance Magic Cow.prototype = new Animal("Hay"); var c = new Cow("White"); c.feed(); // "Hay" var test = c instanceof Animal; // true var test2 = c instanceof Cow; // true
This demo and slides will be available: –http://wildermuth.com Important Links –http://javascript.crawford.com –http://www.ecmascript.org My Company –http://wilderminds.com