SORU
5 Mayıs 2012, CUMARTESİ


Arama "yerel" modül içinde fonksiyon.modülü başka bir işlev verir.ihracat mı?

Nasıl module.exports bildiriminde başka bir işlev içinde bir işlev çağrısı mı?

İşte bazı basit kod.

Benim app.js ben şunlardan:

var bla = require('./bla.js');
console.log(bla.bar());

ve bla.js içimde

module.exports = {

  foo: function (req, res, next) {
    return ('foo');
  },

  bar: function(req, res, next) {
    this.foo();
  }

}

bar ve alıyorum işlevi içinde 7* *işlevine erişmek için çalışıyorum:

TypeError: Object # has no method 'foo'

Eğer this.foo() 11 ** değiştirirsem.

ReferenceError: foo is not defined

CEVAP
9 EKİM 2012, Salı


Modül dışında işlevleri bildirebilirsiniz.ihracat bir blok.

var foo = function (req, res, next) {
  return ('foo');
}

var bar = function (req, res, next) {
  return foo();
}

Sonra:

module.exports = {
  foo: foo,
  bar: bar
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Booredatwork.com

    Booredatwork

    5 Ocak 2009
  • Apple&Tech Reviews & Giveaways

    Apple&Tech R

    12 Temmuz 2008
  • thenewboston

    thenewboston

    4 ŞUBAT 2008