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('\n'));

 

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,三等号
=:赋值,在逻辑运算时也有效;

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

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

 


 

posted on   每当变幻时  阅读(569)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示