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

  • 3DS Max Tutorials

    3DS Max Tuto

    4 AĞUSTOS 2013
  • Michael Lummio

    Michael Lumm

    25 Mayıs 2007
  • NicoleGrippo

    NicoleGrippo

    14 Kasım 2006