Dealing with == and ===

false == 0 or "" == 0 return true.

always use the === and !==

operators that check both the values and the type of the expressions you compare:

var zero = 0;
if (zero === false) {
// not executing because zero is 0, not false
} // antipattern
if (zero == false) {
// this block is executed...
}   

Avoiding eval()

// antipattern
var property = "name";
alert(eval("obj." + property)); // preferred
var property = "name";
alert(obj[property]);

Security implications (e.g. JSON response from an Ajax request)

1. For browsers that don't support JSON.parse() natively, you can use a library from JSON.org.

2. passing strings to setInterval(), setTimeout(), and the Function() constructor is, for the most part, similar to using eval()and therefore should be avoided.

// antipatterns

setTimeout("myFunc()", 1000);
setTimeout("myFunc(1, 2, 3)", 1000); // preferred setTimeout(myFunc, 1000);
setTimeout(function () {
myFunc(1, 2, 3);
}, 1000);   

3. Using the new Function() constructor is similar to eval() and should be approached with care.

    1. If you absolutely must use eval(), you can consider using new Function() instead.
      Because the code evaluated in new Function() will be running in a local function scope, so any variables defined with var in the code being evaluated will not become globals automatically.
    2. Or wrap the eval() call into an immediate function.
      console.log(typeof un); // "undefined"
      
      console.log(typeof deux); // "undefined"
      
      console.log(typeof trois); // "undefined"
      
      var jsstring = "var un = 1; console.log(un);";
      
      eval(jsstring); // logs "1"
      
      jsstring = "var deux = 2; console.log(deux);";
      
      new Function(jsstring)(); // logs "2"
      
      jsstring = "var trois = 3; console.log(trois);";
      
      (function () {
      
          eval(jsstring);
      
      }()); // logs "3"
      
      console.log(typeof un); // "number"
      
      console.log(typeof deux); // "undefined"
      
      console.log(typeof trois); // "undefined" 
    3. No matter where you execute Function, it sees only the global scope. So it can do less local variable pollution.
       (function () {
      
          var local = 1;
      
          eval("local = 3; console.log(local)"); // logs 3
      
          console.log(local); // logs 3
      
      }());
      
      (function () {
      
          var local = 1;
      
          Function("console.log(typeof local);")(); // logs undefined
      
      }()); 

JavaScript Patterns 2.7 Avoiding Implied Typecasting的更多相关文章

  1. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  2. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  3. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  4. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  5. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  6. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  7. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  8. JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns

    In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...

  9. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

随机推荐

  1. Python游戏开发:pygame游戏开发常用数据结构

    一.数组与列表 数组可以理解为简化的列表.像我们之前使用的pygame.sprite.Group这样的精灵组,也是一个列表.列表的元素是可变的,它具有添加.删除.搜索.排序等多种方法. 1.一维列表 ...

  2. CAD利用Select2得到所有实体(网页版)

    主要用到函数说明: IMxDrawSelectionSet::Select2 构造选择集.详细说明如下: 参数 说明 [in] MCAD_McSelect Mode 构造选择集方式 [in] VARI ...

  3. MONO Design创建电信3D机房

    前面我们简单介绍了下一分钟创建3D机房,实则mono Design的功能远远不止这些,试想一下,如果我们花上10分钟来创建一个电信机房,那么MONO design又会给我们带来什么样的惊喜呢? 我们从 ...

  4. 关于Extjs的窗口拖拽,改变大小,背景淡化问题

    大部分Extjs的Windows问题:在Extjs4代码中,只要加几句话: frame:true, //这个窗口的边边是圆的 border : false , //窗口没有边框 draggable: ...

  5. 如何同步iframe与嵌入内容的高度

    最近频繁的做一些通过iframe在a页面嵌入b页面需求.总结下来,有以下问题需要解决 1.如何同步iframe与嵌入内容的高度 2.将b页面载入到a页面后,如何隐藏掉b页面上的元素,如左导航,顶部导航 ...

  6. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...

  7. selenium的调用

    selenium的调用 制作人:全心全意 selenium调用谷歌浏览器 chrome = webdriver.Chrome() //创建谷歌浏览器对象 url="http://www.ba ...

  8. Ubuntu挂载硬盘,修改卷标

    Ubuntu挂载硬盘,修改卷标转载2016-03-06 17:03:21标签:ubuntu Ubuntu不像windows,硬盘插入电脑不会自动读取硬盘 数据,需要把硬盘挂载到文件夹上,然后才能访问硬 ...

  9. 中文情感分析 glove+LSTM

    最近尝试了一下中文的情感分析. 主要使用了Glove和LSTM.语料数据集采用的是中文酒店评价语料 1.首先是训练Glove,获得词向量(这里是用的300d).这一步使用的是jieba分词和中文维基. ...

  10. PatentTips - Hamming distance comparison

    BACKGROUND INFORMATION In a typical data processing environment, data may be transmitted in multiple ...