1

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?

Nathan
  • 6,095
  • 10
  • 45
  • 54
  • 5
    @Chris - that would *cause* the error. – Nick Craver Nov 19 '10 at 20:05
  • Incidentally, this *does* work on FF if I remove the `millis()` call. This may be an error in the Chrome JS interpreter. The `function` definition should be hoisted to the top of the `Layer` function's scope. – cdhowie Nov 19 '10 at 20:06
  • 1
    Works fine in Chrome for me, as long as I comment out the `this.lastup = millis();` line. – user113716 Nov 19 '10 at 20:07
  • @Nick sorry, I meant this.scrub = function scrub()... and by delete i meant remove "scrub;". hasty typing, my apologies. – Chris Nov 19 '10 at 20:08
  • @patrick Well, you obviously have to comment out millis() since you have no such (global) function declared. The OP probably has. – Šime Vidas Nov 19 '10 at 20:12
  • @Šime Vidas - Yes, my point was that I modified the code from the original. I could have modified it instead to include a `mills()` function. I agree that OP probably has that function, which is why I felt it was OK to comment out since it wouldn't be the source of the issue. :o) – user113716 Nov 19 '10 at 20:13
  • 3
    voting to close. OP states he had an error elsewhere. – lincolnk Nov 19 '10 at 20:34

2 Answers2

3

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.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Thats what I meant in my comment, sorry for the confusion. – Chris Nov 19 '10 at 20:07
  • Why would it be incorrect? The function declaration is defined first (by the interpreter), then `this.scrub` gets assigned a reference to that declared function. – user113716 Nov 19 '10 at 20:09
  • @Justin But if he would *just* get rid of the line, then scrub would still be a private function, and therefore not callable as a method. `this.scrub = function() {}` is necessary to make it a callable method. – Šime Vidas Nov 19 '10 at 20:09
  • This is correct apparently, but my actual problem was that elsewhere in the code, I set dummy = new Object(). :( – Nathan Nov 19 '10 at 20:18
  • 2
    @justin there's nothing wrong with the way it's written. I'd prefer the function definition comes first for organization but it doesn't make a difference here. – lincolnk Nov 19 '10 at 20:29
  • @lincolnk But it the OP wants the function to be a method of the instance then the way to do it is as Justin (and I) wrote. The way it's originally written is just confusing (if nothing else). – Šime Vidas Nov 19 '10 at 20:35
  • 2
    @Šime you might prefer it that way, but the original code is technically fine and none of the answers here are going to fix anything. – lincolnk Nov 19 '10 at 20:38
  • @lincolnk I prefer it? Who wouldn't? Why declare a local function and then assign it as a member, if you can assign it as a member directly? – Šime Vidas Nov 19 '10 at 20:40
  • @Nathan - This answer isn't correct. Accepting it will just confuse future readers. You'd be better off deleting the question. – user113716 Nov 19 '10 at 20:52
  • Yeah, I can't delete it though. Sorry to confuse everybody. I voted to close it. – Nathan Nov 19 '10 at 21:00
2

This is how you have to define functions if you want them to be callable "from the outside":

this.scrub = function(npos) { ... }
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385