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 ...
随机推荐
- CAD控件:COM接口实现自定义实体
1. 实现步骤: 3 1. 实现步骤: 参考例子 :Src\MxDraw5.2\samples\ie\iedemoTest.htm 1) 增加自定义实体对象 调用DrawCustomEntity函数, ...
- 第二节:SQLServer导出-重置sa密码-常用sql语句
1.SQLServer导出: 点击要导出数据库----->右键(任务)----->生成脚本----->下一步----->下一步(高级)要编写脚本的数据类型---选择架构和数据 ...
- elk 6.3.2 搭建
CentOS7和java1.8.0) 然后登陆elastic的官网地址下载ELK组件:https://www.elastic.co/cn/products 我是下载了6.3.0版本的: elast ...
- Java基础——工具类
一Java 常用类 Object Object类是所有类.数组.枚举类的父类.位于Java.lang包.也就是说,Java允许把任意类型的对象赋给Object类型的变量. Object类的常用方法 1 ...
- Oracle中的COALESCE,NVL,NVL2,NULLIF函数
http://jingyan.baidu.com/article/fa4125acaf898e28ac7092b9.html
- 登录deepin 15.9后不显示任务栏,无法操作
一直觉得在Linux下编程很酷,所以决定装个Deepin试试,安装很顺利,然后搭建了开发环境,写了一个简单程序,觉得挺不错的. 哪知第二天一开机,登录后找不到任务栏了,做不了啥操作,走接傻眼了,直觉以 ...
- Pycharm Anaconda 安装dlib
由于采用python3.7安装会出现各种问题,两种解决方法. 1)安装Cmake boost等(不推荐,麻烦且不容易成功). 2)安装Anaconda,创建一个python3.6的环境. 这里使用第二 ...
- 爬虫-----HTML解析
对HTML的解析: 在解析复杂的HTML的页面时,需要避免一些问题,好让爬虫工作变得得心应手. • 寻找“打印此页”的链接,或者看看网站有没有HTML样式更友好的移动版(把自己 的请求头设置成处于移动 ...
- gnuplot examples
xy plot #set terminal jpeg #set output 'alfa.jpg' set terminal postscript eps font 24 set out 'U_vs_ ...
- L2-011. 玩转二叉树(不建树)
L2-011. 玩转二叉树 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.这里假设键值都是互不相等的正整 ...