Crockford Douglas JavaScript.ppt

Embed Size (px)

Citation preview

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    1/39

    JavaScript:The Good Parts

    Douglas Crockford

    Yahoo! Inc.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    2/39

    The World's Most

    Misunderstood

    Programming Language

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    3/39

    The broadest range of

    programmer skills of any

    programming language.

    From computer scientists

    to cut-n-pasters

    and everyone in between.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    4/39

    Complaints

    • "JavaScript is not a language I

    know."

    • "The browser programmingexperience is awful."

    • "It's not fast enough."

    • "The language is just a pile of

    mistakes."

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    5/39

    Hidden under a huge

    steaming pile of goodintentions and blunders is

    an elegant, expressive

    programming language.

    JavaScript has good parts.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    6/39

    Influences

    • Java

    syntax

    conventions

    • Self 

    prototypal inheritance

    dynamic objects

    • Scheme

    lambda

    loose typing

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    7/39

    Bad Parts

    • Global Variables

    • + adds and concatenates

    • Semicolon insertion

    • typeof 

    • with and eval

    • phony arrays

    • for..in

    • == and !=

    • false, null, undefined, NaN

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    8/39

    value = myObject[name];

    if (value == null) {

    alert(name + ' not found.');

    }

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    9/39

    value = myObject[name];

    if (value === undefined) {

    alert(name + ' not found.');

    }

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    10/39

    Bad Heritage

    • Blockless statements

    if (foo)

    bar();

    • Expression statements

    this.foo;

    • Floating point arithmetic

    0.1 + 0.2 !== 0.3

    • switch

    • ++ and --

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    11/39

    Good Parts

    • Lambdas

    • Dynamic Objects

    • Loose Typing

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    12/39

    Inheritance

    • Inheritance is object-oriented

    code reuse.

    • Two Schools:• Classical

    • Prototypal

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    13/39

    Prototypal Inheritance

    • Class-free.

    • Objects inherit from objects.

    • An object contains a secret link toanother object.

    var newObject =

    oldObject.begetObject();

    newObject

     __proto__ 

    oldObject

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    14/39

    Prototypal Inheritance

    Object.prototype.begetObject =

    function () {

    function F() {}

    F.prototype = this;

    return new F();

    }

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    15/39

     begetObject method

     prototype

    F

    Object.prototype.begetObject = function () {function F() {}

    F.prototype = this;

    return new F();

    }

    newobject = oldobject.begetObject();

    constructor

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    16/39

     begetObject method

     prototype

    F

    Object.prototype.begetObject = function () {function F() {}

    F.prototype = this;

    return new F();

    }

    newobject = oldobject.begetObject();

    constructor

    oldobject

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    17/39

     begetObject method

     prototype

    F

    Object.prototype.begetObject = function () {function F() {}

    F.prototype = this;

    return new F();

    }

    newobject = oldobject.begetObject();

    constructor

    newobject oldobject

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    18/39

     begetObject method

    Object.prototype.begetObject = function () {function F() {}

    F.prototype = this;

    return new F();

    }

    newobject = oldobject.begetObject();

    newobject oldobject

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    19/39

    new

    • The new operator is required whencalling a Constructor.

    • If new is omitted, there is nocompile-time or run-time warning.

    • The global object is clobbered by

    the constructor.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    20/39

    A Module Pattern

    var singleton = function () {

    var privateVariable;

    function privateFunction(x) {

    ... privateVariable...

    }return {

    firstMethod: function (a, b) {

    ... privateVariable...

    },

    secondMethod: function (c) {

    ... privateFunction()...

    }

    };

    }();

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    21/39

    Closure

    • A function object contains

    A function (name, parameters, body)

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

    • This is a very good thing.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    22/39

    later method

    • The later method causes amethod on the object to be

    invoked in the future.

     my_object.later(1000, "erase", true);

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    23/39

    later method

    Object.prototype.later =

    function (msec, method) {

    var that = this;

    var args = Array.prototype.slice.

    apply(arguments, [2]);

    if (typeof method === 'string') {

     method = that[method];

    }

    setTimeout(function () {

     method.apply(that, args);

    }, msec);

    return that;

    };

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    24/39

    Event Reg

     myObject.

    on('ready', beginProc).

    on('busy', reschedule, [a, b]).on('delete', 'erase');

     myObject.fire({type: ready});

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    25/39

    Event Reg

    function eventreg(o) {

    var handle = {};

    o.on = function (type, method, parameters) {

    var e = {

     method: method,

     parameters: parameters

    };

    if (handler[type]) {

    handler[type].push(e);

    } else {

    handler[type] = [e];

    }

    return o;

    };

    o.fire = function (event) {...};

    o.off = function (type, method) {...};

    return o;

    }

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    26/39

    o.fire = function (event) {...};

    var e, // handler record 

    f, // handler function

    i, // loop index

    h = handler[m.type]; // array of handler records

    if (handler) {

    for (i = 0; i < h.length; i += 1) {

    e = h[i];

    f = e.method;

    if (isString(f)) {

    f = o[f];

    }

    f.apply(this, e.parameters || [event]);

    }

    }

    return o;

    }

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    27/39

    Inheritance Patterns

    • Prototypal Inheritance worksreally well with public methods.

    • Parasitic Inheritance works reallywell with privileged and privateand public methods.

    • Pseudoclassical Inheritance for

    elderly programmers who are oldand set in their ways.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    28/39

    Working with the Grain

    • Pseudoclassical patterns are less

    effective than prototypal patterns

    or parasitic patterns.

    • Formal classes are not needed for

    reuse or extension.

    • Be shallow. Deep hierarchies arenot effective.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    29/39

    A Personal Journey

    Beautiful Code

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    30/39

    JSLint

    • JSLint defines a professional

    subset of JavaScript.

    • It imposes a programmingdiscipline that makes me much

    more confident in a dynamic,

    loosely-typed environment.

    • //http://www.JSLint.com/

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    31/39

    WARNING!

    JSLint will hurt your

    feelings.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    32/39

    Unlearning Is

    Really Hard

    Perfectly Fine == Faulty

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    33/39

    Style Isn't Subjective

    block {

    ....

    }

    • Works well in

    JavaScript

    block

    {

    ....}

    • Might work well

    in other

    languages

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    34/39

    Fixing JavaScript

    • Deprecate the weak features.

    • Fix the blunders carefully.

    • Add new features that do notbreak syntax.

    • Keep it simple. Keep it safe.

    • Make it simpler. Make it safer.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    35/39

    Fixing JavaScript

    • toJSONString and parseJSON

    • a safe eval method

    • object.dontEnum(name)

    • No experiments.

    • No radical changes.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    36/39

    The Very Best Part:

    StabilityNo new design errors

    since 1999!

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    37/39

    More Languages!

    • The world is full of programming

    languages. Why restrict ourselves

    to just JavaScript?

    • We need a classical Ajax

    language for programmers

    without the mental capacity to

    master JavaScript.

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    38/39

  • 8/19/2019 Crockford Douglas JavaScript.ppt

    39/39

    JavaScript

    • It is a really good language if you

    avoid its weaknesses.

    • Don't destabilize the language.• Let's make new languages.

    • This time without so many bad

    parts.