Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript for PHP developers

stoyan
October 09, 2011

JavaScript for PHP developers

stoyan

October 09, 2011
Tweet

Other Decks in Programming

Transcript

  1. DOM/BOM ¤ These days libraries can take care of most pains

    ¤ Let’s focus on the core JavaScript (ECMAScript) language
  2. Assoc arrays var o = { "one": 1, "two": 2

    }; $o = array( "one" => 1, "two" => 2 );
  3. if if (1 === 1) { universe = "fine"; };

    if (1 === 1) { $universe = "fine"; };
  4. switch var a = 1; var result = ""; switch

    (a) { case 1: // strict comparison result = "a is 1"; break; default: result = "@#$"; }
  5. try-catch try { throw new Error('ouch'); } catch (e) {

    msg = e.message; } try { throw new Exception('ouch'); } catch (Exception $e) { $msg = $e->getMessage(); }
  6. while var i = 0, out = ''; while (i

    < 100) { out += ++i + ","; } $i = 0; $out = ''; while ($i < 100) { $out .= ++$i . ","; }
  7. do-while var i = 0, out = ''; do {

    out += ++i + ","; } while (i < 100); $i = 0; $out = ''; do { $out .= ++$i . ","; } while ($i < 100);
  8. for for (var i = 0, out = ''; i

    < 100; i++) { out += i + ','; } for ($i = 0, $out = ''; $i < 100; $i++) { $out .= $i . ','; }
  9. for-in/foreach for (var k in stuff) { keys += k;

    values += stuff[k]; } foreach ($stuff as $k => $v) { $keys .= $k; $values .= $v; }
  10. function function junction(a, b) { return a * b; }

    function junction($a, $b) { return $a * $b; }
  11. function function junction(a, b) { b = b || 2;

    return a * b; } function junction($a, $b = 2) { return $a * $b; }
  12. function function junction(a, b) { b = typeof b !==

    "undefined" ? b : 2; return a * b; } function junction($a, $b = 2) { return $a * $b; }
  13. functions are objects var junction = function (a, b) {

    return a * b; }; junction(3, 4); // 12 junction.length; // 2
  14. functions are objects junction.call(null, 3, 4); // 12 junction.apply(null, [3,

    4]); // 12 call_user_func('junction', 3, 4); call_user_func_array('junction', array(3, 4));
  15. closures var junction = function (a, b) { return a

    * b; }; junction(3, 4); // 12 $junction = function($a, $b) { return $a * $b; }; $junction(3, 4); // 12
  16. function scope $a = function() { $c = 3; $b

    = function($a, $b) { return $c * $a * $b; }; return $b; }; $b = $a(); $b(1, 2); // 0 in PHP, 6 in JS
  17. PHP Class class Dog { var $name; function __construct($name) {

    $this->name = $name; } function getName() { return $this->name; } } $fido = new Dog("Fido"); $fido->getName(); // Fido
  18. JS constructor function function Dog (name) { this.name = name;

    this.getName = function () { return this.name; }; } var fido = new Dog("Fido"); fido.getName();
  19. Constructor and prototype function Dog (name) { this.name = name;

    } Dog.prototype.getName = function () { return this.name; }; var fido = new Dog("Fido"); fido.getName();
  20. Constructor and prototype function Dog (name) { this.name = name;

    } Dog.prototype.getName = function () { return this.name; }; var fido = new Dog("Fido"); fido.getName(); // Fido
  21. Object literals var fido = { name: "Fido", getName: function()

    { return this.name; } }; fido.getName(); // Fido
  22. Object literals var fido = { name: "Fido", getName: function()

    { return this.name; } }; fido.getName(); // Fido
  23. Literals var fido = {}; fido.name = "Fido"; $fido =

    (object) array(); $fido->name = "Fido"; $fido = new stdClass(); $fido->name = "Fido";
  24. Literals in PHP $fido = (object)array(); $fido->name = 'Fido'; $fido->getName

    = function() { return $GLOBALS['fido']->name; }; $method = $fido->getName; echo $method();
  25. Literals in PHP $fido = new JSObject(); $fido->name = 'Fido';

    $fido->getName = function($self) { return $self->name; }; $fido->getName(); // Fido
  26. Literals in PHP class JSObject { function __call($name, $args) {

    if (is_callable($this->$name)) { array_unshift($args, $this); return call_user_func_array($this->$name, $args); } } }
  27. Funny operators ¤  typeof ¤  typeof 1; // "number" ¤ 

    typeof(1); ¤  instanceof ¤  ([1,2]) instanceof Array; // true ¤  ([1,2]) instanceof Object; // true ¤  delete ¤  var o = {a: 1}; delete o.a; o.a; // undefined ¤  void ¤  returns undefined whatever the operand
  28. Global object ¤ something Like $GLOBALS[] but object ¤ May or may

    not be accessible directly ¤ Accessible as window in browsers
  29. 9 global functions ¤ 4 number-related ¤  parseInt() ¤  parseFloat() ¤ 

    isNaN() ¤  isFinite() ¤ 4 to encode/decode URIs ¤  encodeURIComponent() ¤  decodeURIComponent() ¤  encodeURI() ¤  decodeURI() ¤ eval() PHP: intval() floatval() is_nan() is_finite() urlencode() urldecode() ??() ??() eval()
  30. We don’t need no constructors ¤ regular expression literals // yep

    var re = /[a-z]/gmi; // proly nope var re = new RegExp("[a-z]", "gmi");
  31. We don’t need no constructors ¤ functions // yep var f

    = function(a, b) {return a + b;}; // yep function f(a, b) {return a + b;} // nope var f = new Function("a, b", "return a + b;");
  32. We don’t need no constructors ¤ strings // yep var s

    = "confoo"; // nope var s = new String("confoo"); s.substr(3); // "foo" "confoo".substr(0, 4); // "conf"
  33. We don’t need no constructors ¤ numbers // yep var n

    = 1.2345; // nope var n = new Number(1.2345); n.toFixed(2); // 1.23
  34. We don’t need no constructors ¤ boolean // yep var b

    = true; // nope, why would you ever var b = new Boolean(true);
  35. We don’t need no constructors ¤ Errors throw new Error("Ouch"); //

    but you could also throw { name: "MyError", message: "Ouch" };
  36. Constructors ¤  Object() ¤  Array() ¤  RegExp() ¤  Function() ¤ 

    String() ¤  Number() ¤  Boolean() ¤  Error(), SyntaxError()… ¤ Date()
  37. Object.prototype var o = {}; o.toString(); // "[object Object]" o.toLocaleString();

    // "[object Object]" o.valueOf() === o; // true o.hasOwnProperty('toString'); // false o.propertyIsEnumerable('toString'); // false o.isPrototypeOf(Array); // false o.constructor === Object; // true
  38. Array.prototype var a = [1,2,3,4]; a.length; // 4 a.push('dude'); //

    5, the length a.pop(); // "dude" a.unshift('dude'); // 5, the length a.shift(); // "dude" a.concat(5,6,7); // [1,2,3,4,5,6,7]
  39. Array.prototype a.sort(callback); a.indexOf(3); // 2 a.lastIndexOf(3); // 2 a.slice(1, 3);

    // [2, 3] a.splice(...); // remove and add a.reverse(); // [4, 3, 2, 1] a.join(' > '); // implode()
  40. RegExp.prototype var re = /[a-z]/gmi; re.exec("somestring"); // returns matches re.test("somestring");

    // returns true|false re.lastIndex; re.source; // "[a-z]" /[0-9]/g.global; // true /[0-9]/m.multiline; // true /[0-9]/i.ignoreCase; // true
  41. String.prototype var s = "confoo"; s.length; // 6 s.indexOf('o'); //

    1 s.lastIndexOf('o'); // 5 s.charAt(1); // "o" s.charCodeAt(1); // 111
  42. String.prototype s.split(/f/); // ["con", "oo"] s.concat('.ca'); // "confoo.ca" s.search(/foo/); //

    3 s.replace(/o/g, "@"); // c@nf@@ s.match(/[a-z]/g); // ["c", "o", "n", .. s.slice(3, 6); // "foo" s.substring(0, 3); // "con", substr() too
  43. Number.protoype new Number(123).toFixed(2); // "123.00" (1000000000000).toExponential(); // "1e+12" (1000000000000).toPrecision(3); //

    "1.00e+12" Number.MAX_VALUE; // 1.7976931348623157e+308 Number.MIN_VALUE; // 5e-324 Number.POSITIVE_INFINITY; // Infinity Number.NEGATIVE_INFINITY; // -Infinity Number.NaN; // NaN
  44. Math ¤ Not a constructor ¤ Constants Math.E, Math.PI... and 6 more

    ¤ Methods Math.min(), Math.max(), Math.random(), Math.sin() ... and 14 more
  45. Date.prototype ¤ You’re on your own! var d = new Date(2011,

    3, 11); d.getDate(); d.getDay(); d.getFullYear(); d.getHours(); d.getMilliseconds(); d.getMinutes(); d.getMonth(); d.getSeconds(); d.getTime(); d.getTimezoneOffset(); d.getUTCDate(); d.getUTCDay(); d.getUTCFullYear(); d.getUTCHours(); d.getUTCMilliseconds(); d.getUTCMinutes(); d.getUTCMonth(); d.getUTCSeconds(); d.getYear(); d.setDate(); d.setFullYear(); d.setHours(); d.setMilliseconds(); d.setMinutes(); d.setMonth(); d.setSeconds(); d.setTime(); d.setUTCDate(); d.setUTCFullYear(); d.setUTCHours(); d.setUTCMilliseconds(); d.setUTCMinutes(); d.setUTCMonth(); d.setUTCSeconds(); d.setYear(); d.toDateString(); d.toGMTString(); d.toLocaleDateString(); d.toLocaleFormat(); d.toLocaleTimeString(); d.toString(); d.toTimeString(); d.toUTCString(); Date.now(); Date.parse(); Date.UTC();
  46. Constructors ¤  Object() ¤  Array() ¤  RegExp() ¤  Function() ¤ 

    String() ¤  Number() + Math ¤  Boolean() ¤  Error(), SyntaxError()… ¤  Date()