开发者

I Cannot call any object's methods in Javascript [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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) { ... }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜