首页 > 编程语言 > 详细

JavaScript Patterns 2.7 Avoiding Implied Typecasting

时间:2014-05-26 12:46:00      阅读:370      评论:0      收藏:0      [点我收藏+]

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:

bubuko.com,布布扣
var zero = 0;
if (zero === false) {
    // not executing because zero is 0, not false
}

// antipattern
if (zero == false) {
    // this block is executed...
}   
bubuko.com,布布扣

Avoiding eval()

bubuko.com,布布扣
// antipattern
var property = "name"; alert(eval("obj." + property)); // preferred
var property = "name"; alert(obj[property]);
bubuko.com,布布扣

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.

bubuko.com,布布扣
// antipatterns

setTimeout("myFunc()", 1000);
setTimeout("myFunc(1, 2, 3)", 1000);

// preferred

setTimeout(myFunc, 1000);
setTimeout(function () {
    myFunc(1, 2, 3);
}, 1000);   
bubuko.com,布布扣

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.
      bubuko.com,布布扣
      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" 
      bubuko.com,布布扣
    3. No matter where you execute Function, it sees only the global scope. So it can do less local variable pollution.
      bubuko.com,布布扣
       (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
      
      }()); 
      bubuko.com,布布扣

JavaScript Patterns 2.7 Avoiding Implied Typecasting,布布扣,bubuko.com

JavaScript Patterns 2.7 Avoiding Implied Typecasting

原文:http://www.cnblogs.com/haokaibo/p/Avoiding-Implied-Typecasting.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!