douglas crockford 15 oct 08 bayfp
TRANSCRIPT
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
1/48
FunctionalJavaScript
Douglas Crockford
Yahoo! Inc.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
2/48
The World's Most MisunderstoodProgramming Language
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
3/48
A language of many contrasts.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
4/48
The broadest range of
programmer skills of anyprogramming language.
From computer scientiststo cut-n-pasters
and everyone in between.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
5/48
Complaints
"JavaScript is not a language I know."
"The browser programming experience is
awful." "It's not fast enough."
"The language is just a pile of mistakes."
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
6/48
Hidden under a huge steamingpile of good intentions and
blunders is an elegant,
expressive programminglanguage.
JavaScript has good parts.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
7/48
JavaScript is succeeding very
well in an environment whereJava was a total failure.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
8/48
Influences
Self
prototypal inheritance
dynamic objects Scheme
lambda
loose typing
Java
syntax
conventions Perl
regular expressions
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
9/48
Bad Parts
Global Variables
+ adds and concatenates
Semicolon insertion
typeof
with and eval
phony arrays
== and !=
false, null, undefined, NaN
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
10/48
Transitivity? What's That? 0 == '' // true 0 == '0' // true '' == '0' // false false == '' // true false == '0' // true false == undefined // false false == null // false null == undefined // true " \t\r\n " == 0 // true " \t\r\n " == "" // false
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
11/48
Good Parts
Lambda
Dynamic Objects
Loose Typing
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
12/48
(define foo (lambda ( a b c )( body )
)
var foo = function ( a , b , c ) {
return body ;};
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
13/48
( foo a b c )
foo ( a , b , c )
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
14/48
(cond ( p1 e1 ) ( p2 e2 ) ... (else en ))
p1 ? e1 : p2 ? e2 : ... en
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
15/48
(quote ( a b c ))
[' a ', [' b ', [' c ']]]
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
16/48
Y Combinator
var Y = function (le) {return function (f) {
return f(f);}(function (f) {
return le(function (x) {return f(f)(x);
});});};
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
17/48
Inheritance
Inheritance is object-oriented code reuse.
Two Schools:
Classical
Prototypal
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
18/48
Prototypal Inheritance
Class-free.
Objects inherit from objects.
An object contains a link to another object:Delegation. Differential Inheritance.
var newObject =
Object.create(oldObject);newObject
__proto__ oldObject
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
19/48
Objects
Objects are general containers.
Key/value pairs.
Keys are strings.
Values are any value.
Objects can be modified at any time. Objects are passed by reference.
An object can inherit from another object.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
20/48
Prototypes
An objectcontaining
public methods
An object
containinginstance data
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
21/48
Public methods are functions.
A pseudoparameter this is bound to the
invoked object.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
22/48
Object literals
Simple quasiliteral constructor for objects.
{name : value ,name : value
}
Inspiration for the JSON Data InterchangeFormat. www.JSON.org/
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
23/48
Closure
var digit_name = function () {
var names = ['zero', 'one', 'two',
'three', 'four', 'five', 'six',
'seven', 'eight', 'nine'];
return function (n) {
return names [n];};
} () ;
alert( digit_name (3)); // 'three'
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
24/48
A Module Pattern var singleton = function () {
var privateVariable ;function privateFunction (x) {
... privateVariable ...
}return {
firstMethod: function (a, b) {... privateVariable ...
},
secondMethod: function (c) {... privateFunction ()...
} } ;}() ;
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
25/48
Module pattern is easily
transformed into a powerfulconstructor pattern.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
26/48
Power Constructors
1. Make an object.
Object literal
new Object.create
call another power constructor
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
27/48
Power Constructors
1. Make an object.
Object literal, new , Object.create , call
another power constructor 2. Define some variables and functions.
These become private members.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
28/48
Power Constructors
1. Make an object.
Object literal, new , Object.create , call
another power constructor 2. Define some variables and functions.
These become private members.
3. Augment the object with privilegedmethods.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
29/48
Power Constructors
1. Make an object.
Object literal, new , Object.create , call
another power constructor 2. Define some variables and functions.
These become private members.
3. Augment the object with privilegedmethods.
4. Return the object.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
30/48
Step One
function myPowerConstructor(x) {var that = otherMaker(x);
}
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
31/48
Step Two
function myPowerConstructor(x) {var that = otherMaker(x);var secret = f(x);
}
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
32/48
Step Three
function myPowerConstructor(x) {var that = otherMaker(x);var secret = f(x);that.priv = function () {
... secret x that ...};
}
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
33/48
Step Four
function myPowerConstructor(x) {var that = otherMaker(x);var secret = f(x);that.priv = function () {
... secret x ...};
return that;}
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
34/48
Closure
A function object contains
A function (name, parameters, body)
A reference to the environment in which it wascreated (context).
This is a very good thing.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
35/48
Values
Numbers
Strings
Booleans
Objects & Arrays
Functions Falsy values:
false, 0, "", null, undefined, NaN
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
36/48
HistoryThirteen years ago in a valley
30 miles to the south...
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
37/48
Working with the Grain
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
38/48
A Personal JourneyBeautiful Code
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
39/48
JSLint
JSLint defines a professional subset of JavaScript.
It imposes a programming discipline thatmakes me much more confident in adynamic, loosely-typed environment.
http://www.JSLint.com/
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
40/48
WARNING!JSLint will hurt your
feelings.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
41/48
Unlearning Is
Really Hard
Perfectly Fine == Faulty
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
42/48
It's not ignorance does so muchdamage; it's knowin' so derned
much that ain't so.
Josh Billings
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
43/48
The Very Best Part:
StabilityNo new design errors
since 1999!
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
44/48
Coming Soon
[ES3.1] ECMAScript Fourth Edition
Corrections
Reality
Support for object hardening
Strict mode for reliability Waiting on implementations
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
45/48
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
46/48
Safe Subsets
The most effective way to make thislanguage better is to make it smaller.
FBJS Caja & Cajita
ADsafe
These subsets will be informing the designof a new secure language to replaceJavaScript.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
47/48
The Good Parts
Your JavaScript application can reach apotential audience of billions.
If you avoid the bad parts, JavaScriptworks really well. There is some brilliancein it.
It is possible to write good programs withJavaScript.
-
8/7/2019 Douglas Crockford 15 Oct 08 Bayfp
48/48