JakeMesser
JakeMesser

Reputation: 1

Uncaught TypeError: Object #<character> has no method 'Logic'

var Player1 = new character;

When I attempt to call the function with Player1.Logic(); or Logic(); I get an error:

Uncaught TypeError: Object #<character> has no method 'Logic'

Here is my character.js class file:

(function(window){

function character(){
}

function Logic(){
    console.log("new character loaded!");
}

window.character = character;
}(window));

Should I be declaring the function differently or calling it differently? Everything else seems to work fine except for this. The only luck that I've had so far is with declaring functions with event handlers.

Upvotes: 0

Views: 104

Answers (2)

Naftali
Naftali

Reputation: 146310

Well that is because the Logic function has nothing to do with the character function.

And the Logic function is inside the local scope of that IIFE.


Here is something you could do:

var Character = (function(){

    function Character (){}

    Character.prototype.logic = function(){
        console.log("new character loaded!");
    }

    return Character

})();

And you can use it like so:

var myCharacter = new Character();
myCharacter.logic();

Side node: Capitalized methods are usually used for classes and lowercase methods are usually used for regular functions and class methods.

Upvotes: 3

Duncan
Duncan

Reputation: 1550

You're declaring your Logic() function outside of your character() function...

Try this:

(function (window) {

    function character() {
        this.Logic=function() {
            console.log("new character loaded!");
        }
    }

    window.character = character;
}(window));
var Player = new character();
character.Logic();

Upvotes: 0

Related Questions