I Cannot call any object's methods in Javascript [closed]
Im trying to define this class, and later instantiate it and call some of it's methods.
function Layer(){
this.image = null;
this.owned = false;
this.sim = false;
this.pos = 0.5;
this.vel = 0;
开发者_如何学编程 this.acc = 0;
this.lastup = millis();
this.newpos = 0;
this.scrub = scrub;
function scrub(npos){
this.newpos = npos;
this.vel = 0;
this.acc = 0;
}
}
dummy = new Layer();
dummy.scrub(0.8);
// chrome says Uncaught TypeError: Object #<an Object> has no method 'scrub'
Am I defining the methods correctly?
You're not defining your method correctly. Instead of:
this.scrub = scrub;
function scrub(npos){ ... }
It should be:
this.scrub = function(npos){ ... }
Or you could simply get rid of the this.scrub = scrub;
line altogether.
This is how you have to define functions if you want them to be callable "from the outside":
this.scrub = function(npos) { ... }
精彩评论