由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 动态语言把我绕晕了--javascript
相关主题
出个题考考大家:)Why this is a dangling pointer
也问个二维数组的函数传递问题形参可以直接使用私有数据成员?
How to user Perl to handle object on client side?问个参数读入和传递的设计问题
an interview question搞不定,不得不问,一维数组跟二维数组的问题
C++ Q20: construction and inheritance问个copy constructor的问题
Nodejs socket.io emit看不懂其实到今天也没真正搞懂数组参数的传递问题
char *getbuff() 有什么equivalent的写法嘛?Help: DotNet Tcp Client/Server (转载)
[合集] 请问是不是所有编程语言的函数调用,Matlab/C++/Java/Delphnetwork programming dummy question
相关话题的讨论汇总
话题: broadcast话题: client话题: channel话题: 函数话题: senderid
进入Programming版参与讨论
1 (共1页)
n*******s
发帖数: 482
1
Node in Action 的例子代码 讲emitter的
关于这一行
this.on('broadcast', this.subscriptions[id]);
我是不是可以理解成
1. 当channel.emit('join', id, client)时候,'broadcast' event被注册(to be
listened by channel object)
2. 当channel.emit('broadcast', id, data)时候,'broadcast'被emit,因为channel
一直在listen这个event,从而invoke 'this.subscriptions[id]'
3. 实参id, data替换了this.subscriptions[id]里面的形参[senderId, message],函
数可以执行
4. 尽管函数this.subscriptions[id]体内的变量:id并非形参,因为语言是动态的,所
以在执行时候 函数”this.
subscriptions[id]“是知道id的值的,所以函数可以正确执行?
是在觉得怪怪的,请教一下大家。
另外,到底怎么实现广播的呢?肯定是因为每个join的client都在listen 'broadcast'
event,但是从这个写法上看
this.on('broadcast', this.subscriptions[id]);
this 是代表channel object, 很明显channel object只有一个,所有的client都在它
的clients field下面。所以,并不是client listen'broadcast' event, 只有channel
在listen, 似乎因为每个client都join了, 所以这个'broadcast' event被channel关
联到了多个函数上(每个函数都是’this.subscriptions[id]‘),所以在得到'
broadcast'事件时候,所有注册过的函数(this.subscriptions[id]函数族)都被
invoke了。
是这样实现广播的么? 我的理解感觉很靠不住。
var events = require('events')
, net = require('net');
var channel = new events.EventEmitter();
channel.clients = {};
channel.subscriptions = {};
channel.on('join', function(id, client) {
this.clients[id] = client;
this.subscriptions[id] =
function(senderId, message) {
if (id != senderId) {
this.clients[id].write(message);
}
}
this.on('broadcast', this.subscriptions[id]);
});
var server = net.createServer(
function (client) {
var id = client.remoteAddress + ':' + client.remotePort;
client.on('connect',
function() {
channel.emit('join', id, client);
});
client.on('data', function(data) {
data = data.toString();
channel.emit('broadcast', id, data);
});
});
server.listen(8888);
p*****2
发帖数: 21240
2
这个跟动态语言有啥关系呀?
n*******s
发帖数: 482
3
今天早上看到了介绍closure的一句话
One of the most widely used aspects of functional programming in dynamic
languages is the closure, which allows creating a new instance of a function
which retains access to the context in which it was created.
我明白了为甚么当broadcast时候能找到id的值了。
this.subscriptions[id] =
function(senderId, message) {
if (id != senderId) {
this.clients[id].write(message);
}
}
1 (共1页)
进入Programming版参与讨论
相关主题
network programming dummy questionC++ Q20: construction and inheritance
node写起来 感觉很拧巴,时不时写时间长了就好了。这玩意以后真的可以超过php成为第一语言么?Nodejs socket.io emit看不懂
Anybody help me on these questions?char *getbuff() 有什么equivalent的写法嘛?
How does X Input Method work?[合集] 请问是不是所有编程语言的函数调用,Matlab/C++/Java/Delph
出个题考考大家:)Why this is a dangling pointer
也问个二维数组的函数传递问题形参可以直接使用私有数据成员?
How to user Perl to handle object on client side?问个参数读入和传递的设计问题
an interview question搞不定,不得不问,一维数组跟二维数组的问题
相关话题的讨论汇总
话题: broadcast话题: client话题: channel话题: 函数话题: senderid