Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript : Cannot access member function

Tags:

javascript

NEWBIE question.

I cannot access member function. What am I doing wrong?

index.js ->
var abc = require('./def');
var foo = new abc();
foo.zxc(); 

def.js ->
var bar = function(){
// do something
    var zxc = function(){
        // do something
    }
}
module.exports = def;

When I run in brwoser console it shows :

TypeError:foo.zxc is not a function

like image 739
kalpa Avatar asked May 21 '26 16:05

kalpa


1 Answers

Because zxc is just a local variable not accessible from outside of the the bar function. You can change it to

var bar = function() {
   // do something
    this.zxc = function(){
        // do something
    }
}

Now, zxc is an own property of the constructed object so it will work.

like image 134
dfsq Avatar answered May 23 '26 05:05

dfsq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!