JavaScript

El lado OSCURO

¿Por qué?




    Buenas ideas, mal ejecutadas

      Nuevas funcionalidades inconsistentes

      ¿Que Hacer?




      Evitar partes del lenguaje problemáticas

      Evitar fuente de errores difíciles de localizar

      Programar sólo con las partes buenas de JS

      Usa JSLint / JSHint !

      Browser Objects


      • console
      • navigation
      • performance
      • ...


      • Implementación dependiente de navegador
      • Asegurar su disponibilidad lo primero

      Global Object


      • Evitar las variables globales
      • Usar "namespaces"

      (function( window, undefined ) {
          ...
          // Map over jQuery in case of overwrite
          _jQuery = window.jQuery,
          
          // Map over the $ in case of overwrite
          _$ = window.$,
          ...
          jQuery.fn = jQuery.prototype = { ... };
          ...
          window.jQuery = window.$ = jQuery;
          ...
      })( window );

      Palabras Reservadas


      para JavaScript:
      break, case, catch, continue, debugger, default, delete,do, else, false, finally, for, function, if, in, instanceof, new, null, return, switch, this, throw, true, try, typeof, var, void, while, with.
      para el futuro:
      abstract, boolean, byte, char, class, const, double, enum, export, extends, final, float, goto, implements, import, int, interface, let, long, native, package, private, protected, public, short, static, super, synchronized, throws, transient, volatile, yield.
      para el navegador:
      alert, blur, closed, document, focus, frames, history, innerHeight, innerWidth, length, location, navigator, open, outerHeight, outerWidth, parent, screen, screenX, screenY, statusbar, window.



      var method;                  //ok
      var class;                   //illegal
      object = {box: value};       //ok
      object = {case: value};      //illegal
      object = {'case': value};    //ok
      object.box = value;          //ok
      object.case = value;         //illegal
      object.['case'] = value;     //ok

      "arguments"


      function echoArgs() {
          console.log(arguments);         // [object Arguments]
          console.log(arguments[0]);      // 1
          console.log(arguments.slice(0, 1)); // Error!
      }
      echoArgs(1, 2, 3, 4);

      function echoArgs() {
          var slice = Array.prototype.slice;
          return slice.call(arguments, 0, 1);
      }
      echoArgs(1, 2, 3, 4);   //1
      

      eval




      eval('console.log("' + key + '")')


      Inseguro, difícil de leer

      ;



      return;
      {
          status: true
      };
      
      return {
          status: true
      };

      ,


      var array = [
      	'one',
      	'two',
      	'three', // IE error!
      ];
      var array = [
      	'one',
      	'two',
      	'three'
      ];

      parseInt



      parseInt('10');    	// 10
      parseInt('10 cows');	// 10
      parseInt('cows 10');	// NaN
      parseInt('09/20013');	// 0!
      parseInt('08/20013');	// 0!

      parseInt('10', 10);
      parseInt('10 cows', 10);
      parseInt('cows 10', 10);
      parseInt('09/20013', 10);
      parseInt('08/20013', 10);

      delete


      var numbers = [
          'one',
          'two',
          'three',
          'four',
          'five'
      ];
      delete numbers[2];
      numbers.length; // 5!

      NULL




      typeof null // object!!!

      NaN



      NaN === NaN // false
      NaN !== NaN // true
      

      Falsy


      0
      NaN
      ''
      false
      null
      undefined
      {} // nunca!


      0 == '0'    		// true
      0 == ''			// true
      '' == '0'		// false

      false == 'false'	// false
      false == 0 		// true

      false == undefined 	// false
      false == null 		// false
      null == undefined 	// true

      ' \t\r\n' == 0 		// true





      ===

      Float



      0.1 + 0.2
      (0.1*100 + 0.2*100)/100
      









      GRACIAS!





      JavaScript: El lado oscuro

      By Anthanh

      JavaScript: El lado oscuro

      Historias del inframundo relacionadas con el lenguaje JavaScript

      • 1,577