javascript - Uncaught typeerror: Object #<object> has no method 'method' -
so here's object 'playerturnobj'
function playerturnobj(set_turn) { this.playerturn=set_turn; function setturn(turntoset) { this.playerturn=turntoset; } function getturn() { return this.playerturn; } }
and here i'm doing it
var turn = new playerturnobj(); turn.setturn(1);
so try make script setturn() method in playerturnobj() save 'turn' in game i'm making. problem is, not turn.setturn(1); part because keep getting error above
what doing wrong? searched, not find exact answer question.
this not way javascript works. "constructor" function contains inline functions not visible outside of scope of playerturnobj
. variable turn
not have method setturn
defined, error message states correctly. want this:
function playerturnobj(set_turn) { this.playerturn=set_turn; } playerturnobj.prototype = { setturn: function(turntoset) { this.playerturn=turntoset; }, getturn: function() { return this.playerturn; } };
now variable turn
has 2 methods setturn
, getturn
operate on instance created new
.
Comments
Post a Comment