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

  • JorteexHD

    JorteexHD

    20 NİSAN 2012
  • Kai Moosmann

    Kai Moosmann

    5 Temmuz 2006
  • NightShader1

    NightShader1

    25 Temmuz 2006