0

I have this simple Javascript object that calls its own prototype method in one of its local methods. [SEE MY EDIT]

var Obj = function() {
    function inner() {
        this.exported();
    }

    this.exported = function() {
        alert("yay!");
    };
};

var test = new Obj();
Obj.exported();

However, I get the error TypeError: Object function() {...} has no method 'exported'. Any idea how this should be done?

EDIT: whoops, just realized I never called inner(), but thanks Patrick for answering that part anyways. Here is a better example

var Obj = function() {

    this.exported = function() {
        inner();
    };

    function inner() {
        this.yay();
    }

    this.yay = function() {
        alert("yay!");
    };
};

var test = new Obj();
test.exported();

I get TypeError: Object [object global] has no method 'exported'

woojoo666
  • 7,103
  • 6
  • 38
  • 51

2 Answers2

3

should be used

test.exported();

instead of

Obj.exported();

You should invoked exported() method over the test object, not over the Obj constructor.

UPDATE

After reached inside the function inner(){...}. this is refers to global window not Obj, so pass the actual object this from inner(this) and do invoke export() from that passed object into function inner(cthis){...}, something like.

function inner(cthis) {
    //----------^--get Obj on here
    cthis.exported();
}

this.exported2 = function() {
    inner(this);
   //------^--pass Obj from here
};

DEMO

Suman Bogati
  • 5,931
  • 1
  • 18
  • 33
2

to call exported you need to call it off the variable you assigned the instance to:

test.exported();

Also for the inner function you cannot use this there to access the Obj object as it refers to window. save a reference to this and then use that to call the function

var Obj = function() {
    var _this = this;
    function inner() {
        _this.exported();
    }

    this.exported = function() {
        alert("yay!");
    };
};
Patrick Evans
  • 38,456
  • 6
  • 62
  • 84
  • true - but this has nothing to do with the question. `inner()` isn't called anywhere. – Kaii Mar 15 '14 at 21:31
  • but according to [this answer](http://stackoverflow.com/a/3127440/1852456) the `this` refers to the top level object – woojoo666 Mar 15 '14 at 21:39
  • ok I think I get it now, `this` refers to the object prototype, not the current instance of the object? – woojoo666 Mar 15 '14 at 21:47
  • @woojoo666, actually it refers to the global object, but that means it is `window` not Obj. Look at point 3 in that answer, when a function is executed without a context `this` refers to `window`. And since you have to execute `inner` without a context, ie `inner();`, so `this` will be `window`. – Patrick Evans Mar 15 '14 at 21:52
  • I thought inner was executed from the context of Obj, because it is in Obj's scope? – woojoo666 Mar 15 '14 at 22:17
  • 1
    @woojoo666 the function called inner is available on a special scope called closure: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures This is sometimes used to simulate private members. As zerkms pointed out; you're not using prototype and the disadvantage of simulating instance specific private members is that the functions that need to access them can't be put on the prototype. More information about prototype here: http://stackoverflow.com/a/16063711/1641941 – HMR Mar 16 '14 at 00:58