lovecn / lovecn.github.io

个人记录
5 stars 5 forks source link

node.js笔记 #33

Open lovecn opened 9 years ago

lovecn commented 9 years ago
//Node.js使用filesystem模块从磁盘读取文件内容
var fs = requires('fs');
fs.readFile('somefile.txt', 'utf8', function(err, data)
{
if(err) 
    throw err;
console.log(data);
});

var fs = require('fs'), http = require('http');
var options = {
host : 'andrewliu.tk',
port : 80,
path : '/'
};
http.get(options, function(res)
{
console.log("got a response from baidu.com");
}).on('error', function(e)
{
console.log("there was an error from baidu.com");
});
http.createServer(function(req, res)
{
var pathname = url.parse(req.url).pathname;
if(pathname === '/')  //路径为127.0.0.1:3000
{
    res.writeHead(200, 
    {
        'Content-Type' : 'text/plain'
    });
    res.end('Home Page\n')
}
else if(pathname === '/about')//路径为127.0.0.1:3000/about
{
    res.writeHead(200, 
    {
        'Content-Type' : 'text/plain'
    });
    res.end('About Me\n')
}
else if(pathname === '/redirect')//路径为127.0.0.1:3000/redirect
{
    res.writeHead(301, 
    {
        'Location' : '/'
    });
    res.end();
}
else
{
    res.writeHead(404, 
    {
        'Content-Type' : 'text/plain'
    });
    res.end('Page not Found\n')
}
}).listen(3000, "127.0.0.1");

ES5中规定 (http://es5.github.io/#x9.6),数组的长度会使用 toUint32() 转化,即 2^32。
将页面上所有div元素的id值收集到一个javascript数组中:
在回调函数中,this指向每次迭代中的当前DOM元素。
var iDs = $("div").map(function(){
    return (this.id==undefined) ? null :this.id;
}).get();
得到一个用逗号分隔的复选框 ID:
$(':checkbox').map(function() {
return this.id;
}).get().join();
1、遍历数组(有附件参数)
$.each(Array, function(p1, p2){
     this;       //这里的this指向每次遍历中Array的当前元素
     p1; p2;     //访问附加参

}, ['参数1', '参数2']);
2、遍历对象(没有附加参数)

$.each(Object, function(name, value) {

     this;      //this指向当前属性的值

     name;      //name表示Object当前属性的名称

     value;     //value表示Object当前属性的值
});