JavaScript: The Language

23
Fundamental JavaScript: The Language Presented by Brian Dukes

description

An overview of the fundamental features of JavaScript, highlighting the unexpected and obscure features that make it behave different than other languages in the C family.

Transcript of JavaScript: The Language

Page 1: JavaScript: The Language

Fundamental JavaScript: The Language

Presented by Brian Dukes

Page 2: JavaScript: The Language

Where are we going?

• Feature Overview• Syntax• Objects• Functions• Inheritance• Miscellaneous

Page 3: JavaScript: The Language

Feature Overview

• 1st Class Functions• Loose Typing• Dynamic Objects• Prototypal Inheritance• Global Variables

Page 4: JavaScript: The Language

Where are we?

• Feature Overview• Syntax• Objects• Functions• Inheritance• Miscellaneous

Page 5: JavaScript: The Language

Syntax: Primitive Types

• Numbero 64-bit floating, "double"

• Stringo Surrounded by single or double quotes

• Booleano true or false

• Objectso Key/value pairs

• Arrayso Indexed collection

• Functionso Callable code

Page 6: JavaScript: The Language

Syntax: Truthy & Falsy Values

• Falsy values:ofalseonulloundefinedo''o0oNaN

• Truthy values:oEverything elseotrue, 'false', {}

Page 7: JavaScript: The Language

Syntax: Statements & Operators

• var • if• switch• while• for• for in• do while• try• throw• return• break • with

• ternary (? :)• typeof• ||• &&• == & ===• != & !==• +• !

Page 8: JavaScript: The Language

Where are we?

• Feature Overview• Syntax• Objects• Functions• Inheritance• Miscellaneous

Page 9: JavaScript: The Language

Objects: Literals

• JSON (plus functions)oName/value pairsoName is any string

Quotes are optional for legal nameoValue can be any JavaScript value:

Other objectsFunctionsArrays

Page 10: JavaScript: The Language

Objects: Setting & Getting

var flight = {    airline: "Oceanic",    number:  815};alert(flight.airline);alert(flight.equipment); // undefined flight.equipment.status // throw "TypeError" flight.equipment = {    model: 'Boeing 777' };flight['flight-status'] = 'overdue';alert(flight["flight-status"]);

Page 11: JavaScript: The Language

Where are we?

• Feature Overview• Syntax• Objects• Functions• Inheritance• Miscellaneous

Page 12: JavaScript: The Language

Functions: Definition

• Functions are objectsoFunctions have a prototypeoFunctions can have propertiesoFunctions can have methodsoFunctions can be passed as arguments to

functionsoFunctions can be returned from functions

• Functions can be invoked

Page 13: JavaScript: The Language

Functions: Closure

• Functions have access to outer variables•this & arguments are different for each

function 

var i;for (i = 0; i < 10; i++) {    setTimeout(function () {        console.log(i*i);    }, i);}

 

Page 14: JavaScript: The Language

Functions: Scope

• Does not have block scopeoUnlike every other language with C style

syntax• Function scopefunction outer () {    for (var i = 0; i < 10; i++) {        var double = i+i;        $link.click(function () {             alert(double)         });    }} 

Page 15: JavaScript: The Language

Functions: Callbacks

• Functions can be passed to other functions  makeAjaxCall(function (result) {    alert(result); }); 

Page 16: JavaScript: The Language

Functions: Module Pattern

• Call an anonymous function after declaring it (IIFE)

• Keeps inner values from being accessible outside of the function

 (function () {    var x = 1;}());

Page 17: JavaScript: The Language

Where are we?

• Feature Overview• Syntax• Objects• Functions• Inheritance• Miscellaneous

Page 18: JavaScript: The Language

Inheritance: Pseudoclassical

•new operator• Function names are capitalized, by

convention• Lots of downfalls

Page 19: JavaScript: The Language

Inheritance: Prototype

• All objects has a prototype property• When retrieving properties, any property not

on the object is retrieved from its prototype• If the property isn't on the prototype, its

prototype is checked, etc.

Page 20: JavaScript: The Language

Where are we?

• Feature Overview• Syntax• Objects• Functions• Inheritance• Miscellaneous

Page 21: JavaScript: The Language

Miscellaneous: Arrays

• Literal notation: [true, 1, "string", {}]•length property• Not bounded

oUse push method to add to the end• Is a weird object, not a "real" array

Page 22: JavaScript: The Language

Miscellaneous: Regular Expressions

• Pattern Matching• Literal notation: /pattern/options

Page 23: JavaScript: The Language

Resources

• JavaScript Weekly - http://javascriptweekly.com/• JSLint - http://www.jslint.com/• JSLint.VS2010 - http://bit.ly/JSLint-VS2010• jsFiddle - http://jsfiddle.net/• MDC - https://developer.mozilla.org• This - http://www.slideshare.net/EngageSoftware/