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

  • Blendtec

    Blendtec

    30 EKİM 2006
  • Chanre Joubert

    Chanre Joube

    27 Temmuz 2012
  • Jonnyriddlin1

    Jonnyriddlin

    4 Ocak 2007