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

  • Motor Trend Channel

    Motor Trend

    11 Mart 2006
  • Shantanu Sood

    Shantanu Soo

    3 Kasım 2008
  • wolfys you tube

    wolfys you t

    22 Kasım 2006