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. 日常开发需要掌握的Maven知识

    文章来自:https://www.jianshu.com/p/e224a6dc8f20和https://www.jianshu.com/p/20b39ab6a88c Maven出现之前 jar包默认都 ...

  2. 文艺平衡树-splay的区间操作

    真的是个神题,蒟蒻表示无力吐槽.刚开始以为是一个板子题,看着题解打了一遍,大概也理解了他是怎么实现的,然后我就去做别的题了,然后就在Three_D大佬的询问下蒙*了.最后还是问的nc哥,并思考了一个中 ...

  3. LCS(HDU_5495 循环节)

    传送门:LCS 题意:给出两个序列an和bn,想在给出一个序列pn,问经过a[p1],,,,a[pn]和b[p1],,,b[pn]变换后序列a和序列b的最长公共子序列的长度是多少. 思路:对a[i]- ...

  4. python_ 学习笔记(hello world)

    python中的循环语句 循环语句均可以尾随一个else语句块,该块再条件为false后执行一次 如果使用break跳出则不执行. for it in [1,2,3,4]: print(it,end= ...

  5. java容器(数组和集合)内元素的排序问题

    package com.janson.day20180827; import java.util.*; /** * java中容器内对象的排序可以通过Collections.sort()和Arrays ...

  6. MySQL Docker方式安装

    以5.7版本为例 1 配置mysql配置文件编辑/etc/my.cnf,添加以下内容: [mysqld] skip-host-cache skip-name-resolve datadir=/var/ ...

  7. JDBC在Java Web中的应用

    JDBC在Java Web中的应用 制作人:全心全意 在Java Web开发中,JDBC的应用十分广泛.通常情况下,Web程序操作数据库都是通过JDBC实现,即使目前数据库方面的开源框架层出不穷,但其 ...

  8. 深入理解PHP之foreach

    招聘 标签(空格分隔): 招聘 PHP 国贸 语言基础 foreach 语法结构提供了遍历数组的简单方式. php5之前, foreach仅能用于数组php5+, 利用foreach可以遍历对象 fo ...

  9. Vue.Draggable实现拖拽效果(采坑小记)

    之前有写过Vue.Draggable实现拖拽效果(快速使用)(http://www.cnblogs.com/songdongdong/p/6928945.html)最近项目中要用到这个拖拽的效果,当产 ...

  10. [luoguP1474] 货币系统 Money Systems(背包)

    传送门 背包 ——代码 #include <cstdio> #include <iostream> #define LL long long int v, n; LL f[10 ...