JavaScript Patterns 2.7 Avoiding Implied Typecasting
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.
- 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. - 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"
- 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 }());
- If you absolutely must use eval(), you can consider using new Function() instead.
JavaScript Patterns 2.7 Avoiding Implied Typecasting的更多相关文章
- 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 ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- 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 ...
- JavaScript Patterns 5.9 method() Method
Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...
随机推荐
- HDU多校Round 5
Solved:3 rank:71 E. Everything Has Changed #include <bits/stdc++.h> using namespace std; const ...
- 数组array的常用方法简介
数组方法简介 数组总共有22种方法,本文将其分为以下几类来进行详细介绍. 原数组变化:push() pop() shift() unshift() reverse() sort() splice() ...
- NOIP 2018 真・退役记
目录 NOIp 2018 真・退役记 7.01 7.05 \(summary\) 7.12 7.18 7.26 - 7.27 8.2 8.3 8.3 8.7 8.9 8.20 8.24 8.27 8. ...
- MySQL数据库开启、关闭、查看函数功能的方法
应用 MySQL 时,会遇到不能创建函数的情况.出现如下错误信息: ERROR 1418 : This function has none of DETERMINISTIC, NO SQL, or R ...
- 教你如何使用Python写游戏辅助脚本
主要实现方式是通过图片的对比,在游戏中就行点击.运行程序需要以下东西. PIL: 图片处理模块 (python3 换成了 pillow) 下载地址: https://www.lfd.uci. ...
- LINUX-用户和群组
groupadd group_name 创建一个新用户组 groupdel group_name 删除一个用户组 groupmod -n new_group_name old_group_name 重 ...
- cocos2dx luajavaBridge 学习笔记
我在网上看到了 LuaJavaBridge 的 使用方法这篇文章 https://segmentfault.com/a/1190000004252394?utm_source=tuicool& ...
- SGU 485 Arrays
485. Arrays Time limit per test: 1.75 second(s)Memory limit: 262144 kilobytes input: standardoutput: ...
- noip模拟赛 蒜头君打地鼠
分析:直接一个一个地去暴力枚举分数比较少,我们需要一种比较快的统计一定空间内1的数量,标准做法是前缀和,但是二维前缀和维护的是一个矩形内的值,这个是旋转过的该怎么办?可以把图旋转45°,不过这样比较考 ...
- poj 1659 判断是否能构成图Havel-Hakimi定理
//用到了Havel-Hakimi定理,判断是否能够构图 //两种情况不能构图,1:对剩下序列排序后,最大的度数超过了剩下的顶点数 // 2:对最大的度数后面的f个度数减-后,出现了负数 //记录到临 ...