D3

更优的javascript写法

2016-12-22  本文已影响6人  黄清淮
  1. == (或者 !=) 操作在需要的情况下自动进行了类型转换。=== (或 !==)操作不会执行任何转换。===在比较值和类型时,可以说比==更快。
  2. 更简单的indexOf 实现contains功能
var someText = 'javascript rules';
if (someText.indexOf('javascript') !== -1) {
 }
 // or
 if (someText.indexOf('javascript') >= 0) {
 }
建议的方法
1  var someText = 'text';
2  !!~someText.indexOf('tex'); // someText contains "tex" - true
3  !~someText.indexOf('tex'); // someText NOT contains "tex" - false
4  ~someText.indexOf('asd'); // someText doesn't contain "asd" - false
5  ~someText.indexOf('ext'); // someText contains "ext" - true
  1. 更快的取整
    一个位操作符 ~ 将输入的32位的数字(input)转换为-(input+1). 两个位操作符将输入(input)转变为 -(-(input + 1)+1)是一个使结果趋向于0的取整好工具. 对于数字, 负数就像使用Math.ceil()方法而正数就像使用Math.floor()方法. 转换失败时,返回0,这在Math.floor()方法转换失败返回NaN时或许会派上用场。
// 单个 ~
 console.log(~1337)    // -1338
 
 // 数字输入
 console.log(~~47.11)  // -> 47
 console.log(~~-12.88) // -> -12
 console.log(~~1.9999) // -> 1
 console.log(~~3)      // -> 3
 
 // 转换失败
 console.log(~~[]) // -> 0
 console.log(~~NaN)  // -> 0
 console.log(~~null) // -> 0
 
 // 大于32位整数时转换失败
 console.log(~~(2147483647 + 1) === (2147483647 + 1)) // -> 0
  1. 检测对象是否含有某属性
//method1
     var myObject = {
       name: '@tips_js'
     };
     if (myObject.name) { }
 
 //method2
     var myObject = {
       name: '@tips_js'
     };
 
     myObject.hasOwnProperty('name'); // true
     'name' in myObject; // true
 
     myObject.hasOwnProperty('valueOf'); // false, valueOf 继承自原型链
     'valueOf' in myObject; // true
  1. 数组对象插入元素的优化
var arr = [1,2,3,4,5]; 
 //旧方法
 arr.push(6);
 //以下方法比push快将近 50%
 arr[arr.length] = 6;
     
 var arr = [1,2,3,4,5];
 //old method
 arr.unshift(0);  //unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。
 //new method 快98%
 [0].concat(arr);  //concat() 方法用于连接两个或多个数组。
                     //该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。
  1. 嵌套语句优化
//method1
     if (color) {
         if (color === 'black') {
             printBlackBackground();
         } else if (color === 'red') {
             printRedBackground();
         } else if (color === 'blue') {
             printBlueBackground();
         } else if (color === 'green') {
             printGreenBackground();
         } else {
             printYellowBackground();
         }
     }
     
 //method2
     switch(color) {
         case 'black':
             printBlackBackground();
             break;
         case 'red':
             printRedBackground();
             break;
         case 'blue':
             printBlueBackground();
             break;
         case 'green':
             printGreenBackground();
             break;
         default:
             printYellowBackground();
     }
     
 //method3
     switch(true) {
         case (typeof color === 'string' && color === 'black'):
             printBlackBackground();
             break;
         case (typeof color === 'string' && color === 'red'):
             printRedBackground();
             break;
         case (typeof color === 'string' && color === 'blue'):
             printBlueBackground();
             break;
         case (typeof color === 'string' && color === 'green'):
             printGreenBackground();
             break;
         case (typeof color === 'string' && color === 'yellow'):
             printYellowBackground();
             break;
     }
     
 //method4 推荐改为以下方法
     var colorObj = {
         'black': printBlackBackground,
         'red': printRedBackground,
         'blue': printBlueBackground,
         'green': printGreenBackground,
         'yellow': printYellowBackground
     };
     if (color in colorObj) {
       colorObj[color]();
     }
  1. 字符串转数字的更快方法
将字符串转换为数字是极为常见的。最简单和快速的方法是使用+(加号) 来实现。
var one = '1';
var numberOne = +one; // Number 1  
var one = '1';
var negativeNumberOne = -one; // Number -1
//+ 号进行的操作 类似于Number(str) 的操作

2016.12.22

上一篇 下一篇

猜你喜欢

热点阅读