0
JavaScript for PHP
                           developers
                                   The Good Parts

              ...
What this is about
                        the JavaScript language


                        not DOM
                     ...
Am I qualified?

                        Probably not, when it comes to JS
                        Absolutely when it comes...
My JS background
                        First JS usage in the days of NN4
                        Used DynAPI, modified it...
In JavaScript, there is
             a beautiful, elegant,
              highly expressive
                 language...

F...
...that is buried under
               a steaming pile of
             good intentions and
                    blunders.
 ...
Grammar


Freitag, 15. Mai 2009
Names
                        Names can be chosen freely
                        Some words are reserved, most of them not...
Numbers

                        Only one type, 64 bit floating point internally
                        Numbers have metho...
Strings
                        16 bit Unicode, as such somewhat limited
                        Strings have methods and ...
var
                        Most confusing for me was var – it seemed to
                        make no difference most o...
Type issues
                        typeof returns a string with a type
                        For arrays and null it ret...
Functions


Freitag, 15. Mai 2009
Basics

                        Functions are objects, they can be
                          stored in variables, arrays, ...
Defining functions
                        JavaScript has a function statement and a
                        function expre...
Closure
                        Inner functions can access parameters and
                        variables of the context...
var myObject = function () {
                var value = 0;
                return {
                   increment: functio...
Objects


Freitag, 15. Mai 2009
Basics
                        Numbers, strings, booleans, null, undefined are
                        simple types
       ...
Object literals
                        Very cool way of creating objects, confusing if
                        seen for t...
The new statement
                        new creates an object that inherits from the
                        operand, th...
Inheritance
                        Objects inherit from objects, no classes
                        Prototypal inheritenc...
Arrays


Freitag, 15. Mai 2009
Basics
                        Arrays can be created with array literals:
                         var data = [‘one’, ‘two...
Array or Object?
                        Simple rule for what to use:
                        When property names are smal...
Awful & bad parts


Freitag, 15. Mai 2009
Semicolon inerstion
                        JavaScript tries to fix programs by inserting
                        semicolon...
parseInt

                        parseInt(’16’) == parseInt(’16 tons’) == 16
                        parseInt(’08’) retur...
Bitwise operators

                        Avoid them if you think you gain speed
                        All numbers are ...
Read this!
               JavaScript: The Good Parts
                   Douglas Crockford
                        O’Reilly...
Thanks!


Freitag, 15. Mai 2009
Upcoming SlideShare
Loading in...5
×

JavaScript for PHP Developers

2,554

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,554
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
172
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Transcript of "JavaScript for PHP Developers"

  1. 1. JavaScript for PHP developers The Good Parts Karsten Dambekalns <karsten@typo3.org> Freitag, 15. Mai 2009
  2. 2. What this is about the JavaScript language not DOM not browsers not JS frameworks not website building Freitag, 15. Mai 2009
  3. 3. Am I qualified? Probably not, when it comes to JS Absolutely when it comes to PHP Freitag, 15. Mai 2009
  4. 4. My JS background First JS usage in the days of NN4 Used DynAPI, modified it Saw alert() fix timing issues in browsers Was happy when I did not have to use JavaScript anymore Thought I would never touch it again Freitag, 15. Mai 2009
  5. 5. In JavaScript, there is a beautiful, elegant, highly expressive language... Freitag, 15. Mai 2009
  6. 6. ...that is buried under a steaming pile of good intentions and blunders. Douglas Crockford Freitag, 15. Mai 2009
  7. 7. Grammar Freitag, 15. Mai 2009
  8. 8. Names Names can be chosen freely Some words are reserved, most of them not used in JavaScript, though Some word should have been reserved: NaN, Infinity, undefined Freitag, 15. Mai 2009
  9. 9. Numbers Only one type, 64 bit floating point internally Numbers have methods (Most things in JavaScript are objects) Freitag, 15. Mai 2009
  10. 10. Strings 16 bit Unicode, as such somewhat limited Strings have methods and properties toUpperCase(), length Concatenation is done with +, as is addition (Beware of errors as a result of this!) Freitag, 15. Mai 2009
  11. 11. var Most confusing for me was var – it seemed to make no difference most of the time Inside a function var defines a function’s private variables Otherwise variables will be globally accessible! Freitag, 15. Mai 2009
  12. 12. Type issues typeof returns a string with a type For arrays and null it returns ‘object’ typeof NaN === ‘number’ // true! undefined and NaN are global variables – you can change their values Freitag, 15. Mai 2009
  13. 13. Functions Freitag, 15. Mai 2009
  14. 14. Basics Functions are objects, they can be stored in variables, arrays, objects passed as arguments and returned Functions can have methods (they are objects!) Freitag, 15. Mai 2009
  15. 15. Defining functions JavaScript has a function statement and a function expression: function foo() {} means about the same as var foo = function foo() {} Remember: functions are values Freitag, 15. Mai 2009
  16. 16. Closure Inner functions can access parameters and variables of the context in which they are created This can be used to protect variables against manipulation to keep values over function calls Freitag, 15. Mai 2009
  17. 17. var myObject = function () { var value = 0; return { increment: function (inc) { value += typeof inc === ‘number ? inc : 1; } getValue: function () { return value; } }(); Freitag, 15. Mai 2009
  18. 18. Objects Freitag, 15. Mai 2009
  19. 19. Basics Numbers, strings, booleans, null, undefined are simple types Everything else is an object: arrays, functions, regular expressions Objects have properties with name and value Objects are class free Freitag, 15. Mai 2009
  20. 20. Object literals Very cool way of creating objects, confusing if seen for the first time (no new?!) var flight = { airline: ‘FLOW Flights’, number: 3, captain: { ... } } Freitag, 15. Mai 2009
  21. 21. The new statement new creates an object that inherits from the operand, then calls the operand with the created object bound to this If the operand customizes the object but you forgot new, you change global variables! If you use new, make constructor method names UpperCamelCase Freitag, 15. Mai 2009
  22. 22. Inheritance Objects inherit from objects, no classes Prototypal inheritence allows to augment existing objects during runtime: Foo.prototype.bar = function () { ... } Even built-in objects can be enhanced Local properties hide inherited ones Freitag, 15. Mai 2009
  23. 23. Arrays Freitag, 15. Mai 2009
  24. 24. Basics Arrays can be created with array literals: var data = [‘one’, ‘two’, ‘three’]; The length property of an array is the largest integer property name +1 can be set and thus cut off arrays at the end Freitag, 15. Mai 2009
  25. 25. Array or Object? Simple rule for what to use: When property names are small sequential integers, use an array. Otherwise an object. Hard to find out what you have: You need to define a useful is_array method yourself. Freitag, 15. Mai 2009
  26. 26. Awful & bad parts Freitag, 15. Mai 2009
  27. 27. Semicolon inerstion JavaScript tries to fix programs by inserting semicolons – do not rely on that! What does this return? return { status: true }; Freitag, 15. Mai 2009
  28. 28. parseInt parseInt(’16’) == parseInt(’16 tons’) == 16 parseInt(’08’) returns 0 because it assumes octal numbers Always provide the radix parameter to parseInt Freitag, 15. Mai 2009
  29. 29. Bitwise operators Avoid them if you think you gain speed All numbers are floating point, so bitwise operators convert to integer and back Freitag, 15. Mai 2009
  30. 30. Read this! JavaScript: The Good Parts Douglas Crockford O’Reilly Freitag, 15. Mai 2009
  31. 31. Thanks! Freitag, 15. Mai 2009
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×