Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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 为什么需要this.events[type]| |[]行才能正常工作?_Javascript_Node.js - Fatal编程技术网

Javascript 为什么需要this.events[type]| |[]行才能正常工作?

Javascript 为什么需要this.events[type]| |[]行才能正常工作?,javascript,node.js,Javascript,Node.js,伙计们,我正在学习node.js课程,我将要分享的代码工作得非常好。我大部分都能理解。在我正在学习的课程中的这个特定练习中,我们制作了自己的自定义事件侦听器和发射器,以帮助我们更好地了解侦听器和发射器在节点中的工作方式。正如我所说,这在很大程度上是有道理的。我唯一想让大脑思考的部分是Emitter.prototype.on函数表达式主体中的第一行。为什么它必须是this.events[type]=this.events[type]||[]不应此。事件[类型]=[]也工作?我用后者试过了,但效果不

伙计们,我正在学习node.js课程,我将要分享的代码工作得非常好。我大部分都能理解。在我正在学习的课程中的这个特定练习中,我们制作了自己的自定义事件侦听器和发射器,以帮助我们更好地了解侦听器和发射器在节点中的工作方式。正如我所说,这在很大程度上是有道理的。我唯一想让大脑思考的部分是Emitter.prototype.on函数表达式主体中的第一行。为什么它必须是
this.events[type]=this.events[type]||[]不应
此。事件[类型]=[]也工作?我用后者试过了,但效果不好。正如我之前所说,前者确实可以正常工作

我将分享下面的全部代码,但我只想确保它是清楚的。这样做的目的是使多个函数可以添加到events对象的属性数组中。在本例中,我们添加到events对象的属性是“greet”,并且我们将向greet数组添加两个函数。同样,我的问题是,为什么我们必须使用类似于
this.events[type]=this.events[type]|【】这样的行不应
此。事件[类型]=[]也工作?后一行正在创建数组,不是吗?请帮我理解,谢谢

emitter.js代码

function Emitter() {
    this.events = {};
};

Emitter.prototype.on = function(type, listener) {
    this.events[type] = this.events[type] || [];
    this.events[type].push(listener);
} 

Emitter.prototype.emit = function(type){
    if(this.events[type]) {
        this.events[type].forEach(function(listener){
            listener();
        });
    }
}

module.exports = Emitter;
app.js代码

var Emitter = require('./emitter');

var emtr = new Emitter();

emtr.on('greet', function() {
    console.log('Somewhere out there, someone said hello')

});

emtr.on('greet', function() {
    console.log('A Hello has occurred')

});

console.log('hello');
emtr.emit('greet');

this.events[type]=this.events[type]| |[]在这里是一个初始化操作。
第一次注册时,
此.events
为空对象。所以
this.events[type]=[]
是可以的。但当您再次注册
greet
事件时,无法将其重置为[]

// what happened if use this.events[type] = []
var Emitter = require('./emitter');

var emtr = new Emitter();

// ok, this.event['greet'] = []
// this.event['greet'].push(func1)
emtr.on('greet', function() {
    console.log('Somewhere out there, someone said hello')

});

// you reset this.event['greet'] = []
// this.event['greet'].push(func2)
// !!! lost func1 here
emtr.on('greet', function() {
    console.log('A Hello has occurred')

});

console.log('harro');
emtr.emit('greet');

this.events[type]=this.events[type]| |[]在这里是一个初始化操作。
第一次注册时,
此.events
为空对象。所以
this.events[type]=[]
是可以的。但当您再次注册
greet
事件时,无法将其重置为[]

// what happened if use this.events[type] = []
var Emitter = require('./emitter');

var emtr = new Emitter();

// ok, this.event['greet'] = []
// this.event['greet'].push(func1)
emtr.on('greet', function() {
    console.log('Somewhere out there, someone said hello')

});

// you reset this.event['greet'] = []
// this.event['greet'].push(func2)
// !!! lost func1 here
emtr.on('greet', function() {
    console.log('A Hello has occurred')

});

console.log('harro');
emtr.emit('greet');

它不起作用,因为如果将
this.events[type]
设置为一个空数组,当您发出多个事件时,它将为每个事件类型只保留一个侦听器(最后注册的一个)

函数发射器(){
this.events={};
};
Emitter.prototype.on=函数(类型,侦听器){
此.events[type]=[];
this.events[type].push(侦听器);
} 
Emitter.prototype.emit=函数(类型){
if(this.events[类型]){
this.events[type].forEach(函数(侦听器){
监听器();
});
}
}
var emtr=新发射器();
emtr.on('greet',function(){
//log('某个地方,有人打了招呼')
});
emtr.on('greet',function(){
//console.log('发生了Hello')
});

console.log(emtr.events)它不起作用,因为如果将此.events[type]
设置为一个空数组,当您发出多个事件时,它将为每个事件类型保留一个侦听器(最后注册的一个)

函数发射器(){
this.events={};
};
Emitter.prototype.on=函数(类型,侦听器){
此.events[type]=[];
this.events[type].push(侦听器);
} 
Emitter.prototype.emit=函数(类型){
if(this.events[类型]){
this.events[type].forEach(函数(侦听器){
监听器();
});
}
}
var emtr=新发射器();
emtr.on('greet',function(){
//log('某个地方,有人打了招呼')
});
emtr.on('greet',function(){
//console.log('发生了Hello')
});


console.log(emtr.events)
该条件表示如果this.events[type]
是真的,则使用它,否则为它分配一个空数组。一个空数组怎么会是一样的?条件是如果
this.events[type]
是真的,那么就使用它,否则就给它分配一个空数组。一个空数组怎么会一样呢?谢谢!这个回答真的帮助了我。那么它怎么知道传递的第一个函数应该放在数组中呢?我想我现在明白我最后一个问题的答案了<代码>this.events[type]=this.events[type]| |[]
基本上是说
this.events[type]
等于
this.events[type]
内存中的值,并且第一次调用'emtr.on('greet',…)'时
this.events[type]
this.events[type].push(监听器)之前,内存中没有值行。所以
this.events[type]=this.events[type]| |[]
已将其设置为this.events[type].push(侦听器)之前的空数组将其第一个值添加到其中。这花了我一段时间,但你的话真心实意地帮助我让它在我的脑海里点击。谢谢!这个回答真的帮助了我。那么它怎么知道传递的第一个函数应该放在数组中呢?我想我现在明白我最后一个问题的答案了<代码>this.events[type]=this.events[type]| |[]
基本上是说
this.events[type]
等于
this.events[type]
内存中的值,并且第一次调用'emtr.on('greet',…)'时
this.events[type]
this.events[type].push(监听器)之前,内存中没有值行。所以
this.events[type]=this.events[type]| |[]
已将其设置为this.events[type].push(侦听器)之前的空数组将其第一个值添加到其中。这花了我一段时间,但你的话真心实意地帮助我让它在我的脑海里点击。谢谢!这个响应以及上面的响应确实帮助了我。那么它如何知道传递的第一个函数应该被放入数组中呢?你是什么意思?谁知道呢?当我用emtr.on方法创建第一个greet函数时,这个first greet函数值是如何自动放入数组的?不是自动的,但你做到了。当您调用
this.even时,它被放入数组中