Javascript For C# Developers Module 1: Javascript Basics: Shawn Wildermuth Wilder Minds
Javascript For C# Developers Module 1: Javascript Basics: Shawn Wildermuth Wilder Minds
Shawn Wildermuth
Wilder Minds
wilderminds.com
Agenda
JavaScript and C#
Runtime Environments
Comparing Languages
Dynamic Typing
Language Basics
Types
using System; Requiring Libraries
C# and .NET
Just-in-Time Compilation
(et al.)
Comparing Runtime Environments
JavaScript Engines
Depends on Browser
Services
Memory Management
Type System
(et al.)
Comparing Runtime Environments
C# JavaScript
• Strongly-Typed • Loosely-typed
• Static • Dynamic
• Classical Inheritance • Prototypal
• Classes • Functions
• Constructors • Functions
• Methods • Functions
// JavaScript
C#
var customer = new Customer(); Compiler
Variable Inferred
Declaration
Strong and Loose Typing
Strong Typing
Types are defined by names and typically static structure
// C#
var x = 0; // Infers type to be int
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
// C#
class Dog : Animal, IMoveable {...}
class Car : Vehicle, IMoveable {...}
// JavaScript
// JavaScript
var x = {
name: "Shawn",
city: "Atlanta"
};
x.phone = "404-555-1212";
x.makeCall = function () {
callSomeone(this.phone);
};
Language Basics
Global Scope
Objects at root are 'global'
// C#
JavaScript
var x =class
public 1; MyApp
{
function
static someFunction(arg1,
void SomeFunction(int
arg2)
x, int
{ y)
} {
}
someFunction(1,x);
static void Main()
{
var x = 1;
SomeFunction(5, x):
}
}
Language Basics
Type Coalescing
JavaScript Wants to Coalesce Values
// JavaScript
C#
"test " + "me" // test//metest me
"test " + 1
1.ToString();
// test//
1 test 1
"test
String.Concat(100,
" + true //
"25");
test//true
10025
"test " + (1 == 1) // test true
100 + "25" // 10025
Language Basics
// JavaScript
"hello" == "hello"; // true
1 == 1; // true
1 == "1"; // true
Language Basics
// JavaScript
"hello" == "hello"; // true
1 == 1; // true
1 == "1"; // true
1 === "1"; // false
1 !== "1"; // true
1 === 1.000000000000001; // false
1 === 1.0000000000000001; // true
Types
Primitives
JavaScript has basic types
"Value Types"
boolean
string
number
"Reference Type"
object
"Delegate Type"
function
Special
"undefined"
Types
Type Detection
typeof operator
// JavaScript
var x = 1;
var typeName = typeof x; // "number"
Types
Special Types
null
undefined
// JavaScript
var a = null; // "null"
var b = c; // "undefined"
Types
"Value Types"
// JavaScript
var a = typeof 1; // "number"
var b = typeof 1.0; // "number"
var c = typeof true; // "boolean"
var d = typeof false; // "boolean"
var e = typeof "hello world"; // "string"
Types
// JavaScript
var a = 1; // integer
var b = 1.5; // floating point
var c = 070; // integer (in octal)
var d = 0xffff; // integer (in hex)
var e = 1.34e6; // Scientific Notation (1340000)
var f = 10.0; // integer (optimization)
Types
number
Special Values
// JavaScript
var a = Number.MIN_VALUE;
var b = Number.MAX_VALUE;
var c = Number.POSITIVE_INFINITY;
var d = Number.NEGATIVE_INFINITY;
// JavaScript
if (true) {} // true
if (false) {} // false
if ("hello") {} // true
if ("") {} // false
if (25) {} // true
if (0) {} // false
if (10/0) {} // false (NaN)
var a = null;
if (a) {} // false
if (c) {} // false (undefined)
Types
// JavaScript
var s = "String"; // Simple Strings
var t = 'String'; // Either delimiter
log(u.length); // 6
var d = "�;"בָּרוּ // Unicode
log(d.length); // 8 (count of 8 bits)
Types
"Reference Types"
// JavaScript
var a = new Object(); // "object"
var b = new Array(); // "object"
Types
Object Literals
Shortcut for creating data structures
// JavaScript
var data = {
name: "hello",
"some value": 1
};
var more = {
"moniker": "more data",
height: 6.0,
subData: {
option: true,
flavor: "vanilla"
}
};
Types
Array Literals
Shortcut for creating collections
// JavaScript
var array = [ "hello", "goodbye" ];
var coll = [{
"moniker": "more data",
height: 6.0,
subData: {
option: true,
flavor: "vanilla"
}
}];
Types
Array
Untyped Collection
// JavaScript
var c = [];
Functions
Yup, a data type
// JavaScript
var f = function (arg1, arg2) {
};
f(1,2);
var o = {
name: "Shawn",
sing: function (song) {
}
};
o.sing("happy birthday");
Summary