36
JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc.

JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Embed Size (px)

Citation preview

Page 1: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

JavaScript: The First Parts

Part One

Douglas Crockford

Yahoo! Inc.

Page 2: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc
Page 3: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc
Page 4: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc
Page 5: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc
Page 6: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Learn to Program

• Values

• Variables

• Expressions

• Branching

• Loops

• Functions

• Recursion

• Arrays

• Objects

• Trees

Page 7: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Why should you learn to program?

Page 8: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

In programming we construct and maintain extremely complex systems with

aspirations of perfection.

Page 9: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Ulterior Motives

Page 10: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc
Page 11: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc
Page 12: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Programming Languages

Page 13: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

JavaScript

The world's most misunderstood programming

language.

Page 14: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Learn to Program

• Values

• Variables

• Expressions

• Branching

• Loops

• Functions

• Recursion

• Arrays

• Objects

• Trees

Page 15: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Values

Numbers, strings, booleans

null

undefined

Page 16: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Variables

Values stored under names.

The values can be replaced.

Page 17: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Expressions

Elements of computation

Page 18: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Branching

Alter the sequential flow of the program

Page 19: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Loops

Repetitive operations

Page 20: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Functions

Encapsulated expressions

Page 21: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Recursion

Functions defined in terms of themselves

Page 22: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Arrays

Linear sequences of storage

Page 23: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Objects

Associative collections of named values

Page 24: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Trees

Complex structures composed of objects

Page 25: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Inside every programming language there is a calculator

• Literal numbers with infix operators

• * used for multiplication

• / is used for division

• Precedence

• ( ) can change precedence

2 + 3 * 4 === 14

(2 + 3) * 4 === 20

Page 26: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

The Yellow Box

http://jsmvhs.crockford.com/yellowbox.html

Page 27: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Try these

0.3 - 0.1

0.3 - 0.1 - 0.1 - 0.1

1 / 0

• JavaScript numbers don't always work like real numbers.

Page 28: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

The result of an expression can be stored

in a variablea = 3 + 4

Page 29: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

a = a + 1

Page 30: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

The naming of variables is a serious matter

• It isn't just one of your holiday games.

• Well chosen names can make a program self descriptive.

• A variable name should begin with a letter.

• It can contain any number of letters and digits.

• It can contain the _ (underline) character.

Page 31: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Functions

• Functions can encapsulate an expression.

• Functions can be stored in variables.

• Functions may have parameters.

• Functions can be invoked.

• Functions can grow the language.

Page 32: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

function (parameter) { return expression;}double = function (number) { return number * 2;}

double(212)

Page 33: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

function (parameter) { return expression;}celsius = function (f) { return (f - 32) * 5 / 9;}

boiling_point = celsius(212)

Page 34: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

function (parameter) { return expression;}

quad = function (x) { return double(x) + double(x);}

average = function (a, b) { return (a + b) / 2; }

Page 35: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Functions

• A function can take many parameters, or no parameters.

• If it takes more than one parameter, the parameter names are separated with commas.

• A parameter name is like a variable inside the function.

• When a function is called, the argument values are assigned to the parameters.

Page 36: JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc

Assignment One

1. Read Preface, Chapter One of The Good Parts

2. Write function fahrenheit(c)

3. Write a function of your own that takes two or more parameters.

4. Using trial and error, determine the temperature such that

fahrenheit(x) === celsius(x)

Put your answers in a text or html file and add it to http://groups.yahoo.com/group/jsmvhs/files/Assignment1/

Use your unique ID in the name of the file.