js基础知识(pomelo阅读)

0,node.js调试:
http://www.noanylove.com/2011/12/node-the-inspector-debugging-node-js/
 
1,读取配置文件:
var json = require('./configs/hot_deployer.json')
官网描述:

If the exact filename is not found, then node will attempt to load the required filename with the added extension of .js, .json, and then .node .js files are interpreted as JavaScript text files, and .json files are parsed as JSON text files. .node files are interpreted as compiled addon modules loaded with dlopen.

所以直接写一个json的配置文件用require就可以读入直接变成对象使用了,很方便。
 
关于node.js的模块加载机制:
http://www.infoq.com/cn/articles/nodejs-module-mechanism
 
2,关于js中function的参数arguments 
arguments本身并不是数组而是对象,所以想按照数组一样操作它,需要转换,下面是MDN上的解释:
The arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length. For example, it does not have the pop method. However it can be converted to a real Array: (转化成数组的方式)
var args = Array.prototype.slice.call(arguments);

OR

var args = [].slice.call(arguments);
3,查看对象所有属性
log调试的时候想看一个对象的全部属性,所以:
var array = [];
for(var o in msg){
     array.push(o);
}
console.log(array.join('
'));
4,js中双叹号:
相当于三元运算符,返回boolean值。
原理:第一个感叹号是将其转化成Boolean类型的值,但是这一操作得到的是其取反以后的值,在进行一次取反运算才能得到其对应真正的布尔值
var ret = !!document.getElementById

等价于:

var ret = document.getElementById ? true : false;

例子:

var a = " "; alert(!!a);   //true
var a = "s"; alert(!!a);   //true
var a = true; alert(!!a);   //true
var a = 1; alert(!!a);   //true
var a = -1; alert(!!a);   //true
var a = -2; alert(!!a);   //true
 
var a = 0; alert(!!a);   //false
var a = ""; alert(!!a);   //false
var a = false; alert(!!a);   //false
var a = null; alert(!!a);   //false
其他技巧:
var1+""转为string
~~var1转为int
1*var1转为float
[var1]转为array

5,三等号
=:赋值,在逻辑运算时也有效;

==:等于运算,但是不比较值的类型;

===:完全等于运算,不仅比较值,而且还比较值的类型,只有两者一致才为真。

 
原文地址:https://www.cnblogs.com/killbug/p/3407862.html