JavaScript doesn’t have block scope
Block doesn’t have scope in javascript, only function has scope.

for(var i = 0; i < 2; i ++) { } i;  // 2

If you want to create scope, use anonymous function:

(function (){ for(var i = 0; i < 2; i ++) { } })(); typeof(i) === 'undefined';  // true

NaN is weird
NaN indentifies a value which is not a number.
It’s weird because comparing NaN with anything (even NaN itself) is always false.

NaN === NaN; // false

Therefore, the following code may confuse somebody:

parseInt('hello', 10); // NaN parseInt('hello', 10) == NaN; // false parseInt('hello', 10) === NaN; // false

Then, how to check whether a number is NaN or not?
A built-in function can help you, check it:

isNaN(parseInt('hello', 10)); // true

Truthy and falsy values
All values in javascript can be converted into boolean implied.
Note that the following values can be converted into false ones automatically, which called falsy values:
null, undefined, NaN, 0, ‘’, false
Therefore you don’t need to do like this:

if(obj === undefined || obj === null) { }

Simply do as follows:

if(!obj) { }

How to modify arguments
In this post , I give a method to convert arguments into a real array.
Then, how to modify arguments?
For example, add a new value to the end of the arguments:

function add() { arguments.push('new value'); } add(); // error - arguments.push is not a function

Of course there would be an error, because arguments is not a real array.
Following is the solution:

function add() { Array.prototype.push.call(arguments, 'new value'); return arguments; } add()[0]; // "new value"

Boolean and new Boolean
You’d better consider Boolean as a constructor function. It can be used to produce a Boolean literal value:

Boolean(false) === false; // true Boolean('') === false; // true

Boolean(0) is equal to !!0, which was mentioned in this post.
Also, we can use new keyword to produce a boolean object:

new Boolean(false) === false; // false new Boolean(false) == false; // true typeof(new Boolean(false)); // "object" typeof(Boolean(false)); // "boolean"

This is interesting.

JavaScript tips and tricks - 4相关推荐

  1. 转:45 Useful JavaScript Tips, Tricks and Best Practices

    原文来自于:http://flippinawesome.org/2013/12/23/45-useful-javascript-tips-tricks-and-best-practices/ 1 – ...

  2. 从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks(附资源)

    来源:AI公园 本文约2700字,建议阅读10分钟 本文与你分享参加了39个Kaggle比赛的技巧和经验. 想象一下,如果你能得到所有的tips和tricks,你需要去参加一个Kaggle比赛.我已经 ...

  3. 从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks

    本文转载自AI公园. 作者:Derrick Mwiti 编译:ronghuaiyang 导读 作者参加了39个Kaggle比赛,总结了非常多的技巧和经验,现在全部分享给大家. 想象一下,如果你能得到所 ...

  4. Juniper JUNOS Commands (Tips and Tricks)

    Juniper Networks has a Day one book for 'JunOS Tips, Techniques, and Templates 2011' in Junos Fundam ...

  5. 采购配置SAP Purchasing Configuration Tips and Tricks

    采购配置SAP Purchasing Configuration Tips and Tricks   Goods Receipts/Invoice Receipts  for Purchase Ord ...

  6. Remote Development Tips and Tricks

    目录 SSH tips# Configuring key based authentication# Quick start: Using SSH keys# Improving your secur ...

  7. Cisco IOS Command Tips and Tricks – Part 2

    Last updated on August 9, 2019 Cisco IOS command list is getting longer , and it has been split into ...

  8. 【Kaggle】从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks

    作者:Derrick Mwiti 编译:ronghuaiyang 转载:AI公园 导读 作者参加了39个Kaggle比赛,总结了非常多的技巧和经验,现在全部分享给大家. 想象一下,如果你能得到所有的t ...

  9. Linux shell tips and tricks

    2019独角兽企业重金招聘Python工程师标准>>> I'm using Linux shell (Bash) on daily basis, but I often forgot ...

最新文章

  1. 被面试官问懵B了,十亿级数据ES搜索怎么优化?
  2. msbuild 语法_用于删除文件的MSBuild Task语法
  3. Html5音乐可视化之音乐的获取和播放
  4. c语言编程 验证用户名和密码是否正确(函数定义),《C语言程序设计教程》习题参考答案[精品资料].doc...
  5. 开源备份web_13个开源备份解决方案
  6. android阴影分割线,android shape的使用及渐变色、分割线、边框、半透明阴影
  7. pythonajax学习_python学习-- django 2.1.7 ajax 请求
  8. 吴恩达|机器学习作业7.1.主成分分析(PCA)
  9. 增强幸福感的五种方法
  10. Nginx学习日记第五篇 -- upstream及fastcgi
  11. ubuntu 18.04使用aqt安装QT5.12
  12. nest.js实战之集成Linkedin登录
  13. web前端学习(总结/心得)
  14. 迅捷PDF编辑器怎么编辑修改PDF文件?
  15. 10 个win10上记事本的替代品
  16. 详细解说Windows 8.1与Windows 8的区别(Win8.1与Win8区别)
  17. dax和m的区别_德国股票指数DAX, MDAX与TecDAX的区别是什么?
  18. 【微信小程序】粤语教学平台-粤言粤语
  19. 网站动态背景线条跟随鼠标移动,吸附鼠标效果
  20. 如何实现Odoo兼容Citus实现更多数据储存

热门文章

  1. 单片机直接驱动段式液晶
  2. Codeforces Round #183 (Div. 2) C
  3. 各模组相关interface
  4. lisp点转为vla_一种将Region转为Polyline的方法
  5. python爬虫实例电商_价值上千元的Python爬虫外包案例,学会你就赚了
  6. 人工智能的两条进化路线
  7. 是否可能有一台算力无穷大的计算机?
  8. 神经网络的物理学解释(一)---权重与概率幅
  9. java如何定义一个字符栈_Java性能优化之字符串优化处理
  10. java经典50题_JAVA经典算法50题(3)【面试+工作】