码迷,mamicode.com
首页 > Web开发 > 详细

Node.js(三)【HTTP、作用域、上下文对象】

时间:2016-04-03 13:12:42      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

querystring参数小利器
querystring.stringify({name:‘lqc‘,course:[‘jade‘,‘node‘],from:‘‘})
//输出‘name=lqc&course=jade&course=node&from=‘

querystring.stringify({name:‘lqc‘,course:[‘jade‘,‘node‘],from:‘‘},‘,‘)
//输出‘name=lqc,course=jade,course=node,from=‘

querystring.stringify({name:‘lqc‘,course:[‘jade‘,‘node‘],from:‘‘},‘,‘,‘:‘)
//输出‘name:lqc,course:jade,course:node,from:‘

//反序列化,解析url的querystring字符串
querystring.parse(‘name=lqc&course=jade&course=node&from=‘)
//输出{ name: ‘lqc‘, course: [ ‘jade‘, ‘node‘ ], from: ‘‘ }

//转义:
querystring.escape(‘哈哈‘)
//输出‘%E5%93%88%E5%93%88‘

//反转义:
querystring.unescape(‘%E5%93%88%E5%93%88‘)
//输出‘哈哈‘

查看chrome浏览器的缓存
输入网址:chrome://net-internals/#dns

HTTP知识填坑:
状态码:status code

200
400
401
403
404
500
503

HTTP模块
HTTP事件回调
1.回调(把函数作为参数传递进去function add(callback) {console.log(callback)}function callback() {return
‘I am callback‘;}add(callback());)

function add(callback) {var value = callback(); console.log(‘Excuse me, ‘ + value)}
function callback() {return‘I am callback‘;}
add(callback);

callback.js文件:

 1 /**
 2  * 回调
 3  * */
 4 function learn(something){
 5     console.log(something);
 6 }
 7 
 8 function we(callback, something) {
 9     something += ‘ is cool‘;
10     callback(something);
11 }
12 
13 we(learn, ‘Nodejs‘);

//命令行执行此文件
node callback

1 //传递一个匿名函数
2 we(function (something) {
3     console.log(something);
4 }, ‘JavaScript‘);

2.同步/异步

 1 var c = 0;
 2 
 3 function printIt() {
 4     console.log(c);
 5 }
 6 
 7 function plus() {
 8     //延迟1s
 9     setTimeout(function () {
10         c += 1;
11     }, 1000);
12 }
13 
14 plus();
15 printIt();//结果为0

 

/*************************************************************/
//异步执行

 1 var c = 0;
 2 
 3 function printIt() {
 4     console.log(c);
 5 }
 6 
 7 function plus(callback) {
 8     //延迟1s
 9     setTimeout(function () {
10         c += 1;
11         callback();
12     }, 1000);
13 }
14 
15 plus(printIt);

 

3.I/O
4.单线程/多线程
5.阻塞/非阻塞
6.事件
7.事件驱动
8.基于事件驱动的回调
9.事件循环

HTTP源码解读
作用域

 1 ar globalVariable = ‘This is global variable‘;
 2 
 3 function globalFunction () {
 4     //局部变量
 5     var localVariable = ‘This is local variable‘;
 6     console.log(‘Visit global/local variable‘);
 7     console.log(globalVariable);
 8     console.log(localVariable);
 9 
10     globalVariable = ‘This is changed variable‘;
11     console.log(globalVariable);
12 
13     function localFunction () {
14         var innerLocalVariable = ‘This is inner local variable‘;
15         console.log(‘Visit local function‘);
16     }
17 
18     localFunction();
19 }
20 
21 globalFunction();

 

上下文

 1 //声明一个对象变量
 2 var pets = {
 3     words: ‘...‘,
 4     name: ‘ErBao‘,
 5     kind: ‘rabbit‘,
 6     speak: function () {
 7         console.log(this.words); // ...
 8         console.log(this === pets); // true
 9     }
10 };
11 
12 pets.speak();

 

/*************************************/

1 function pets(words) {
2     this.words = words;
3 
4     console.log(this.words);
5     console.log(this); //这里的this并不是pets,而是顶层的global对象
6     console.log(this === global); // true
7 }
8 
9 pets(‘...‘);

this上下文对象是谁取决于函数的调用方式:1.直接调用(this表示全局对象) 2.实例化对象调用(this表示实例化的那个对象)

/*********************************/

 1 function Pets(words) {
 2     this.words = words;
 3     this.speak = function () {
 4         console.log(this.words);
 5         console.log(this);
 6     };
 7     console.log(this);
 8 
 9     /**
10      Pets { words: ‘ErBao‘, speak: [Function] }
11      ErBao
12      Pets { words: ‘ErBao‘, speak: [Function] }*/
13 }
14 
15 //定义一个实例对象
16 var rabbit = new Pets(‘ErBao‘);
17 rabbit.speak();

/***********************************************/

call(),apply()

 1 var pets = {
 2     words: ‘...‘,
 3     speak: function (say) {
 4         console.log(say + this.words);
 5     }
 6 };
 7 
 8 //pets.speak(‘Speak ‘);
 9 
10 var dog = {
11     words: ‘wang‘
12 };
13 
14 //call()方法实现JavaScript继承,改变执行上下文
15 //父对象.父对象的方法.call(子对象, 父对象方法的参数值),父对象的方法继承给子对象
16 //pets对象转换为dog对象,同时调用pets对象的方法
17 pets.speak.call(dog, ‘Speak ‘); // Speak wang

 

/***********************************************/

call、apply实现继承
//定义一个构造函数

 1 //定义一个构造函数
 2 function Pets(words) {
 3     this.words = words;
 4 
 5     this.speak = function () {
 6         console.log(this.words);
 7     };
 8 }
 9 
10 function Dog(words) {
11     Pets.call(this, words);
12     //Pets.apply(this, []);
13 }
14 
15 var dog = new Dog(‘HuaHua‘);
16 dog.speak(); //HuaHua

 

Node.js(三)【HTTP、作用域、上下文对象】

标签:

原文地址:http://www.cnblogs.com/lqcdsns/p/5349496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!