goodparts

59
JavaScript: The Good Parts Douglas Crockford Yahoo! Inc. http://www.crockford.com/ codecamp/

Upload: damonjablons

Post on 04-Dec-2014

1.108 views

Category:

Technology


0 download

DESCRIPTION

This presentation is by Doug Crockford, I'm reposting it here from this Google Blog post: http://googlecode.blogspot.com/2009/03/doug-crockford-javascript-good-parts.html

TRANSCRIPT

Page 1: Goodparts

JavaScript: The Good Parts

Douglas Crockford

Yahoo! Inc.

http://www.crockford.com/codecamp/

Page 2: Goodparts

The World's Most Popular Programming Language

Page 3: Goodparts

The World's Most Popular Programming Language

The World's Most Unpopular Programming Language

Page 4: Goodparts

A language of many contrasts.

Page 5: Goodparts

The broadest range of programmer skills of any programming language.

From computer scientists to cut-n-pasters

and everyone in between.

Page 6: Goodparts

It is the language that people use without bothering to

learn it first.

Page 7: Goodparts

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."

Page 8: Goodparts

Hidden under a huge steaming pile of good

intentions and blunders is an elegant, expressive

programming language.

JavaScript has good parts.

Page 9: Goodparts

JavaScript is succeeding very well in an environment where

Java was a total failure.

Page 10: Goodparts

Influences

• Self

prototypal inheritance

dynamic objects

• Scheme

lambda

loose typing

• Java

syntax

conventions

• Perl

regular expressions

Page 11: Goodparts

Bad Parts

• Global Variables

• + adds and concatenates

• Semicolon insertion

• typeof

• with and eval

• phony arrays

• == and !=

• false, null, undefined, NaN

Page 12: Goodparts

Transitivity? What's That?• '' == '0' // false

• 0 == '' // true

• 0 == '0' // true

• false == 'false' // false

• false == '0' // true

• false == undefined // false

• false == null // false

• null == undefined // true

• " \t\r\n " == 0 // true

Page 13: Goodparts

value = myObject[name];

if (value == null) {

alert(name + ' not found.');

}

Two errors that cancel each other out.

Page 14: Goodparts

value = myObject[name];

if (value === undefined) {

alert(name + ' not found.');

}

Page 15: Goodparts

Good features that interact badly

• Objects can inherit from other objects.

• Functions can be members of objects.

• for..in statement mixes inherited functions with the desired data members.

Page 16: Goodparts

for in is troublesome

• Design question: Should for..in do a shallow skim or a deep dredge?

• Decision: Deep dredge. The programmer must explicitly filter out the deep members.

• Except: They didn't tell anybody!

• Consequence: Lots of confusion about how to use for..in.

Page 17: Goodparts

for in is troublesome

• Better Decision: Don't release the language broadly until we have enough experience to have confidence that we made the right choice.

• Historical Context: Getting it right at Netscape wasn't an option.

Page 18: Goodparts

Bad Heritage

• Blockless statements if (foo)

bar();

• Expression statements foo;

• Floating point arithmetic 0.1 + 0.2 !== 0.3

• ++ and --

• switch

Page 19: Goodparts

Good Parts

• Lambda

• Dynamic Objects

• Loose Typing

• Object Literals

Page 20: Goodparts

Inheritance

• Inheritance is object-oriented code reuse.

• Two Schools:

• Classical

• Prototypal

Page 21: Goodparts

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

Page 22: Goodparts

Prototypal Inheritance

if (typeof Object.create !== 'function') {

Object.create = function (o) {

function F() {}

F.prototype = o;

return new F();

};

}

Page 23: Goodparts

new

• The new operator is required when calling a Constructor function.

• If new is omitted, the global object is clobbered by the constructor.

• There is no compile-time or run-time warning.

Page 24: Goodparts

Global

var names = ['zero', 'one', 'two',

'three', 'four', 'five', 'six',

'seven', 'eight', 'nine'];

var digit_name = function (n) {

return names[n];

};

alert(digit_name(3)); // 'three'

Page 25: Goodparts

Slow

var digit_name = function (n) {

var names = ['zero', 'one', 'two',

'three', 'four', 'five', 'six',

'seven', 'eight', 'nine'];

return names[n];

};

alert(digit_name(3)); // 'three'

Page 26: Goodparts

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'

Page 27: Goodparts

A Module Patternvar singleton = (function () {

var privateVariable;

function privateFunction(x) {

...privateVariable...

}

return {

firstMethod: function (a, b) {

...privateVariable...

},

secondMethod: function (c) {

...privateFunction()...

}

};

}());

Page 28: Goodparts

Module pattern is easily transformed into a powerful

constructor pattern.

Page 29: Goodparts

Power Constructors

1. Make an object.

• Object literal

• new

• Object.create

• call another power constructor

Page 30: Goodparts

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.

Page 31: Goodparts

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 privileged methods.

Page 32: Goodparts

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 privileged methods.

4. Return the object.

Page 33: Goodparts

Step One

function myPowerConstructor(x) { var that = otherMaker(x);}

Page 34: Goodparts

Step Two

function myPowerConstructor(x) { var that = otherMaker(x); var secret = f(x);}

Page 35: Goodparts

Step Three

function myPowerConstructor(x) { var that = otherMaker(x); var secret = f(x); that.priv = function () { ... secret x that ... };}

Page 36: Goodparts

Step Four

function myPowerConstructor(x) { var that = otherMaker(x); var secret = f(x); that.priv = function () { ... secret x that ... }; return that;}

Page 37: Goodparts

Closure

• A function object contains

A function (name, parameters, body)

A reference to the environment in which it was created (context).

• This is a very good thing.

Page 38: Goodparts

Style Isn't Subjective

block

{

....

}

• Might work well in other languages

block {

....

}

• Works well in JavaScript

Page 39: Goodparts

Style Isn't Subjective

return

{

ok: false

};

• SILENT ERROR!

return {

ok: true

};

• Works well in JavaScript

Page 40: Goodparts

Style Isn't Subjective

return

{

ok: false

};

Page 41: Goodparts

Style Isn't Subjective

return; // semicolon insertion

{

ok: false

};

Page 42: Goodparts

Style Isn't Subjective

return;

{ // block

ok: false

};

Page 43: Goodparts

Style Isn't Subjective

return;

{

ok: false // label

};

Page 44: Goodparts

Style Isn't Subjective

return;

{ // useless

ok: false // expression

}; // statement

Page 45: Goodparts

Style Isn't Subjective

return;

{

ok: false; // semicolon

}; // insertion

Page 46: Goodparts

Style Isn't Subjective

return;

{

ok: false;

}; // empty statement

Page 47: Goodparts

Style Isn't Subjective

return;

{ // unreachable statement

ok: false;

}

Page 48: Goodparts

Style Isn't Subjective

return

{

ok: false

};

• Bad style

return;

{

ok: false;

}

• Bad results

Page 49: Goodparts

Working with the Grain

Page 50: Goodparts

A Personal Journey

Beautiful Code

Page 51: Goodparts

JSLint

• JSLint defines a professional subset of JavaScript.

• It imposes a programming discipline that makes me much more confident in a dynamic, loosely-typed environment.

• http://www.JSLint.com/

Page 52: Goodparts

WARNING!JSLint will hurt your

feelings.

Page 53: Goodparts

Unlearning Is Really Hard

Perfectly Fine == Faulty

Page 54: Goodparts

It's not ignorance does so much damage; it's knowin' so

derned much that ain't so.

Josh Billings

Page 55: Goodparts

The Very Best Part:

StabilityNo new design errors

since 1999!

Page 56: Goodparts

Coming Soon

• [ES3.1] ECMAScript Fifth Edition

• Corrections

• Reality

• Support for object hardening

• Strict mode for reliability

"use strict";

• Waiting on implementations

Page 57: Goodparts

Safe Subsets

• The most effective way to make this language better is to make it smaller.

• FBJS

• Caja & Cajita

• Web Sandbox

• ADsafe

• The next edition of ECMAScript might include a secure formal subset.

Page 58: Goodparts

The Good Parts

• Your JavaScript application can reach a potential audience of billions.

• If you avoid the bad parts, JavaScript works really well. There is some brilliance in it.

• It is possible to write good programs with JavaScript.

Page 59: Goodparts