CNode

这有段不是特别懂。。

Ccooldrine发布于13 年前最后回复13 年前11 回复5542 浏览0 收藏
http.get({ host: 'shapeshed.com' }, function(res) {
      console.log("Got a response from shapeshed.com");
  }).on('error', function(e) {
      console.log("There was an error from shapeshed.com");
  });


).on('error', function(e)

一个点,一个on,代表啥意思。。 function(e) 这回调函数在这里有麻用 = =

查看回复

回复 (11)

B
bigmusic#2·13 年前

把对象的每一个方法最后都return this就可以无限链下去,比如

var a={
    b:'bbb',
    c:function(){
    	//doSomething
    	return this;
    },
    d:function(){
    	//doAnotherThing
    	return this;
    }
}

这样就可以a.c().d().c().d().....etc 当然你举的例子是function,不过function也是对象,一样也可以有属性和方法,function是比较有趣的变量~~

H
hexie#3·13 年前

监听 error 时间,收到事件后,执行匿名函数function(e){} e 是错误事件的参数

I
inetfuture#4·13 年前

.表示调用对象的方法,onaddListener的别名,用于注册事件监听器的,eventEmitter.addListener('err', function (e) {})就是监听eventEmitter(事件发生器,这里就是http.get()方法返回的对象)的error事件,事件发生后你传递的匿名函数function (e) {}会被调用,并且会得到一个参数e

M
myy#5·13 年前

这么看: http.get().on(...).on(...),说明 http.get() 这个函数返回的对象有 on() 这个方法可以调,同时,on() 方法也返回对象本身,所以可以继续 on()

专业名词应该叫 链式调用。

J
jiyinyiyong#6·13 年前

感觉可以写点 jQuery 习惯一下 http://developer.51cto.com/art/201008/218553.htm

M
myy#7·13 年前

分开写就是:

var a = http.get(...);

a.on('error', func_err);

function func_err(e) { }

C
cooldrine#10·13 年前
H
halfblood#11·13 年前

http.get().on(...).on(...) : 函数式语言的标准用法。

参与回复
登录后即可参与回复。登录