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'

1
  • 1
    There are no any prototype functions in your code btw. Commented Mar 15, 2014 at 21:31

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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!");
    };
};

6 Comments

true - but this has nothing to do with the question. inner() isn't called anywhere.
but according to this answer the this refers to the top level object
ok I think I get it now, this refers to the object prototype, not the current instance of the object?
@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.
@woojoo666 the function called inner is available on a special scope called closure: 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: stackoverflow.com/a/16063711/1641941
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.