JavaScript: The Good Parts Part Eight: Style - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

JavaScript: The Good Parts Part Eight: Style

Description:

var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine' ... Elements of JavaScript Style. http://javascript.crockford.com ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 27
Provided by: douglasc
Category:
Tags: javascript | eight | good | part | parts | style

less

Transcript and Presenter's Notes

Title: JavaScript: The Good Parts Part Eight: Style


1
JavaScript The Good PartsPart Eight Style
  • Douglas Crockford
  • douglas_at_crockford.com

2
Bad Parts
  • Global Variables
  • adds and concatenates
  • Semicolon insertion
  • typeof
  • with and eval
  • phony arrays
  • and !
  • false, null, undefined, NaN

3
  • value myObjectname
  • if (value null)
  • alert(name ' not found.')

4
  • value myObjectname
  • if (value undefined)
  • alert(name ' not found.')

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

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

7
Bad Heritage
  • Blockless statements
  • if (foo)
  • bar()
  • Expression statements
  • foo
  • Floating point arithmetic
  • 0.1 0.2 ! 0.3
  • and --
  • switch

8
Global
  • var names 'zero', 'one', 'two',
  • 'three', 'four', 'five', 'six',
  • 'seven', 'eight', 'nine'
  • var digit_name function (n)
  • return namesn
  • alert(digit_name(3)) // 'three'

9
Slow
  • var digit_name function (n)
  • var names 'zero', 'one', 'two',
  • 'three', 'four', 'five', 'six',
  • 'seven', 'eight', 'nine'
  • return namesn
  • alert(digit_name(3)) // 'three'

10
Closure
  • var digit_name (function ()
  • var names 'zero', 'one', 'two',
  • 'three', 'four', 'five', 'six',
  • 'seven', 'eight', 'nine'
  • return function (n)
  • return namesn
  • ())
  • alert(digit_name(3)) // 'three'

11
Style Isn't Subjective
  • block
  • ....
  • Works well in JavaScript
  • block
  • ....
  • Might work well in other languages

12
Style Isn't Subjective
  • return
  • ok true
  • Works well in JavaScript
  • return
  • ok false
  • SILENT ERROR!

13
Style Isn't Subjective
  • return
  • ok false

14
Style Isn't Subjective
  • return // semicolon insertion
  • ok false

15
Style Isn't Subjective
  • return
  • // block
  • ok false

16
Style Isn't Subjective
  • return
  • ok false // label

17
Style Isn't Subjective
  • return
  • // useless
  • ok false // expression
  • // statement

18
Style Isn't Subjective
  • return
  • ok false // semicolon
  • // insertion

19
Style Isn't Subjective
  • return
  • ok false
  • // empty statement

20
Style Isn't Subjective
  • return
  • // unreachable statement
  • ok false

21
Style Isn't Subjective
  • return
  • ok false
  • Bad style
  • return
  • ok false
  • Bad results

22
It is possible to write excellent programs in
JavaScript.
  • It requires discipline.

23
Unlearning Is Really Hard
  • Perfectly Fine Faulty

24
It's not ignorance does so much damage it's
knowin' so derned much that ain't so.
  • Josh Billings

25
Write Code to be Read
  • Conventions of spacing, punctuation, and
    formatting are as important in good programming
    as in good writing.

26
Recommended Reading
  • Code Conventions for the JavaScript Programming
    Language
  • http//javascript.crockford.com/code.html
  • The Elements of JavaScript Style
  • http//javascript.crockford.com/style1.html
  • http//javascript.crockford.com/style2.html
Write a Comment
User Comments (0)
About PowerShow.com