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 ...
随机推荐
- UI设计四要素
信息.样式.布局.交互. +层次: UI所有的工作都可以从这几个方面入手.
- jmeter接口测试小结
摘自:http://www.cnblogs.com/houzhizhe/p/6839736.html JMeter做http接口压力测试 测前准备 用JMeter做接口的压测非常方便,在压测之前我们需 ...
- Flask框架 之重定向、cookie和session
一.URL重定向(redirect) @app.route("/login") def login(): # 使用url_for函数通过视图函数的名字找到url路径 url = u ...
- Linux 安装 JDK 详解
安装 JDK 说明:Linux 系统中安装软件需在 root 用户下进行. (1) 首先下载 jdk-8u131-linux-x64.rpm (2)将用户切换至 root,在 opt 文件夹下新建 s ...
- js之DOM间接操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- idea 快捷键设置
通过 点击放大镜然后按当前需要修改的快捷键找到需要修改的快捷键,更改成希望的快捷键
- Spring @Conditional注解 详细讲解及示例
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xcy1193068639/article/details/81491071 前言: @Conditi ...
- 页面加载即执行JQuery的三种方法
[1]$(function( ){ }): $(function(){ $("#name").click(function(){ //adding your code here } ...
- 小a和uim之大逃离(洛谷 1373)
题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电,一阵阵雷声.刹那间,狂风大作,乌云布满了天空,紧接着豆大的雨点从天空中打落下来,只见前方出现了一个 ...
- 稍微成型点的用WEBSOCKET实现的实时日志LOG输出
难的是还是就地用JS显示出来相关的发布进度. 还好,花了一下午实现了. 可以移植到项目中去罗... websocket.py: import tornado.ioloop import tornado ...