Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点事件发射器示例_Javascript_Node.js_Events_Observer Pattern - Fatal编程技术网

Javascript 节点事件发射器示例

Javascript 节点事件发射器示例,javascript,node.js,events,observer-pattern,Javascript,Node.js,Events,Observer Pattern,我认为,这是一个非常简单的例子,在使用Node的EventEmitter时,我可能会使用Observer模式。也就是说,我仍然在用Node做我的海腿。当我与另一位开发人员一起回顾这篇文章时,它看起来是对的,但似乎有点奇怪 奇怪的是灯泡对象是如何监听LightSwitch对象的。好吧,代码是有效的,但我觉得我做错了 /*jslint node: true */ 'use strict'; var events = require('events'); var util = require('u

我认为,这是一个非常简单的例子,在使用Node的EventEmitter时,我可能会使用Observer模式。也就是说,我仍然在用Node做我的海腿。当我与另一位开发人员一起回顾这篇文章时,它看起来是对的,但似乎有点奇怪

奇怪的是灯泡对象是如何监听LightSwitch对象的。好吧,代码是有效的,但我觉得我做错了

/*jslint node: true */

'use strict';

var events = require('events');
var util = require('util');

var LightSwitch = function(state) {
    var self = this;
    self.state = typeof state !== 'undefined' ? state : 'off';
};

util.inherits(LightSwitch, events.EventEmitter);

LightSwitch.prototype.changeState = function() {
    var self = this;
    // FIXME use if else
    if (self.state == 'on') {
        self.state = 'off';
        self.emit('off');
    } else if (self.state == 'off') {
        self.state = 'on';
        self.emit('on');
    }
};

var LightBulb = function(LightSwitch) {
    var self = this;
    self.state = 'off';
    self.switch = LightSwitch;

    self.switch.on('on', function() {
        self.state = 'on';
    });

    self.switch.on('off', function() {
        self.state = 'off';
    });
};

LightBulb.prototype.currentState = function() {
    var self = this;
    console.log('The switch is ' + self.switch.state);
    console.log('The light bulb is ' + self.state);
    console.log('\n');
}

var hallwayLightSwitch = new LightSwitch();

var hallwayLightBulb = new LightBulb(hallwayLightSwitch);

// Default!
console.log('--------- EVERYTHING IS OFF --------------');
console.log('Switch is ' + hallwayLightBulb.switch.state);
console.log('Bulb is ' + hallwayLightBulb.state);
console.log('\n');

// Flick it!
console.log('------------ LIGHTS ON! ------------------');
hallwayLightSwitch.changeState();
hallwayLightBulb.currentState();

// Flick it - again!
console.log('------------ LIGHTS OFF! -----------------');
hallwayLightSwitch.changeState();
hallwayLightBulb.currentState();

// And, flick it - again!
console.log('------------ LIGHTS ON! -----------------');
hallwayLightSwitch.changeState();
hallwayLightBulb.currentState();
然后控制台输出为。。。正如我所料:

[terryp@jinx] playing_with_events (master) :: node 02_lightswitch.js
--------- EVERYTHING IS OFF --------------
Switch is off
Bulb is off


------------ LIGHTS ON! ------------------
The switch is on
The light bulb is on


------------ LIGHTS OFF! -----------------
The switch is off
The light bulb is off


------------ LIGHTS ON! -----------------
The switch is on
The light bulb is on

这对我来说很有意义。。。开关被抛出的“事件”是触发灯泡打开或关闭的事件;这正是它在现实生活中的工作方式。在这里,你需要小心,在灯泡构造器中的lightswitch引用之前,没有“new”。尝试修改测试脚本创建几个灯泡对象,然后尝试关闭其中一个,观察其他对象的情况。