【问题标题】:nodejs : How can I fill a global Array containing all objects of a kind?nodejs:如何填充包含所有对象的全局数组?
【发布时间】:2017-06-14 16:56:41
【问题描述】:

这是我的问题:

我有一个班级机器人。每次创建此类的对象时,我都想填充它。然后将其显示在玉文件上。到目前为止我有:

//bot.js
var listeBots = [];
exports.listeBots = listeBots;

// Constructeur
function Bot (nom, probavoyager, probatweet, probaretweet, nbhashtag, probalien, probalike, probamention, probaselfmention, visibilite,probaphoto) {
    this.nom = nom;
    this.probavoyager = probavoyager;
    this.probatweet = probatweet;
    this.probaretweet =probaretweet;
    this.nbhashtag = nbhashtag;
    this.probalien = probalien;
    this.probalike = probalike;
    this.probamention = probamention;
    this.probaselfmention = probaselfmention;
    this.visibilite = visibilite;
    this.probaphoto = probaphoto;
}

exports.BotSuiveur = function(name){
    Bot.call(this, name, 0.2, 0.2, 0.8, 1, 0.4, 0.8, 0.8, 0.2, 0.05, 0.1 );
    listeBots.push(this);
    //console.log(this);
};

exports.BotLeader = function(name){
    Bot.call(this, name, 0.2, 0.8, 0.4, 2, 0.6, 0.4, 0.4, 0.8, 0.8, 0.2 );
    listeBots.push(this);
    //console.log(this);
};

function BotVoyageur() {
    Bot.call(this, 0.8, 0.6, 0.4, 5, 0.4, 0.4, 0.4, 0.8, 0.15, 0.8 );
    //console.log(this);
}

// jade file
each kw in listeBots
            li= kw

我的翡翠文件只显示了 Object[Object] 等等 如何获取翡翠中可见的对象值?

提前致谢! 罗曼

【问题讨论】:

  • 为什么不迭代它并显示它的值?
  • 你是什么意思?我确实在我的玉代码中对其进行了迭代
  • listeBots 中的每个 kw li= kw
  • 可以附上你的玉码吗?感谢您的回复。
  • 我做到了。谢谢你的帮助

标签: arrays node.js object express pug


【解决方案1】:

最好使用带有new关键字的构造函数,尽量不要在函数中使用this关键字作为变量。

//bot.js
var listeBots = [];
exports.listeBots = listeBots;

// Constructeur
function Bot (nom, probavoyager, probatweet, probaretweet, nbhashtag, probalien, probalike, probamention, probaselfmention, visibilite,probaphoto) {
    this.nom = nom;
    this.probavoyager = probavoyager;
    this.probatweet = probatweet;
    this.probaretweet =probaretweet;
    this.nbhashtag = nbhashtag;
    this.probalien = probalien;
    this.probalike = probalike;
    this.probamention = probamention;
    this.probaselfmention = probaselfmention;
    this.visibilite = visibilite;
    this.probaphoto = probaphoto;
}

exports.BotSuiveur = function(name){
    /* creates bot instance */
    var bot = new Bot(name, 0.2, 0.2, 0.8, 1, 0.4, 0.8, 0.8, 0.2, 0.05, 0.1 );
    listeBots.push(bot);
    //console.log(bot);
};

exports.BotLeader = function(name){
    /* creates bot instance */
    var bot = new Bot(name, 0.2, 0.8, 0.4, 2, 0.6, 0.4, 0.4, 0.8, 0.8, 0.2 );
    listeBots.push(bot);
    //console.log(bot);
};

function BotVoyageur() {
    /* creates bot instance */
    var bot = new Bot('name', 0.8, 0.6, 0.4, 5, 0.4, 0.4, 0.4, 0.8, 0.15, 0.8 );
    //console.log(bot);
}

【讨论】:

  • 好的,谢谢,实际上它和其他语言非常相似^^ 我会试试这个,如果我能解决这个问题,请告诉你。
  • 好的,多亏了你的解释,它似乎已经解决了,我还在构造函数中删除了“this”参数。谢谢 ;) 我可能会在稍后重新发布其他问题^^
猜你喜欢
  • 2023-02-01
  • 2013-05-14
  • 1970-01-01
  • 2015-05-27
  • 2013-01-08
  • 2012-08-16
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
相关资源
最近更新 更多