【问题标题】:Use array objects to print out in JS event listeningJS事件监听中使用数组对象打印输出
【发布时间】:2019-08-06 22:42:24
【问题描述】:

此代码应从要在事件中使用的数组中获取值。 我怎样才能做到这一点?

相反,我所做的是复制代码以在事件中使用并打印给定的值。

这是有效的:

const eventConfig = require('./config').events

const util = require('util');

function x() {
    this.z = 'Hello world';

}


util.inherits(x, EventEmitter)


x.prototype.y = function(event1, event2 ) {
    console.log(this.z + ' ' + event1, event2);
    this.emit(eventConfig.foo, event1, event2)
}


const greeter1 = new x();

x1.on(eventConfig.foo, function(event1, event2){
    console.log('message: ' + event1, event2)
})

x1.y('Tony', 'liam');

这就是我想要做的事情

const eventConfig = require('./config').events

const util = require('util');

function x() {
    this.z = 'Hello world';

}
moethods and properties
util.inherits(x, EventEmitter)

let arguments = ['Tony', 'liam']

x.prototype.y = function(arguments) {
    console.log(this.z);
    this.emit(eventConfig.foo, + arguments)
}

const x1 = new x();

x1.on(eventConfig.foo, function(arguments){
    console.log('message: ' + arguments)
})

x1.y(arguments);

预期结果:

message: Tony, Liam

收到的结果:

message: NaN

【问题讨论】:

    标签: javascript arrays dom-events


    【解决方案1】:

    这是笔误

    
    const util = require('util');
    
    function x() {
        this.z = 'Hello world';
    
    }
    moethods and properties
    util.inherits(x, EventEmitter)
    
    let arguments = ['Tony', 'liam']
    
    x.prototype.y = function(arguments) {
        console.log(this.z);
        //this.emit is an object taking 2 arguments. I was trying to concatenate them
        this.emit(eventConfig.foo, arguments)
    }
    
    const x1 = new x();
    
    x1.on(eventConfig.foo, function(arguments){
        console.log('message: ' + arguments)
    })
    
    x1.y(arguments);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      相关资源
      最近更新 更多