0


I have a problem with my javascript code since I started changing it to an OOP approach. I want to create an Object class with many class functions within and the class functions should be able to call each other. But this is not working. Here is a (simpified) code snippet:

function MyClass(){
    this.someClassVariable = false;
    this.anotherClassVariable = 10;
}

MyClass.prototype = {
    constructor: MyClass,

   firstFunction:function(){
       if (this.someClassVariable == false){
           this.secondFunction();
       }
   },

   secondFunction:function(){
       while(this.anotherClassVariable != 0){
           this.anotherClassVariable--;
           console.log(this.anotherClassVariable);
       }
   }
}

var myClass = new MyClass();
navigator.getUserMedia({audio: true}, myClass.firstFunction, function(e) {});

This always gets me the the error

Uncaught TypeError: __tracer.traceFunCall(...) is not a function

But when I call the Class and Function without getUserMedia() like this:

var myClass = new MyClass();
myClass.firstFunction();

it works.
Am I missing something? Would be really glad if someone could give me a hint, searched a lot for this specific topic but didn't find anything.

Thanks!

3
  • 2
    What about myClass.firstFunction.bind(myClass) ? Commented Jul 30, 2015 at 10:16
  • here myClass.firstFunction you pass reference to function, so when this would be called - this inside would be not referes to myClass variable Commented Jul 30, 2015 at 10:17
  • possible duplicate of Scoping problem with Javascript callback Commented Jul 30, 2015 at 10:17

1 Answer 1

0

Try out

navigator.getUserMedia({audio: true}, myClass.firstFunction.bind(myClass), function(e) {});
Sign up to request clarification or add additional context in comments.

2 Comments

Please elaborate your code-snippet with some explanation.
this didn't solve the problem unfortunately, still getting the error message

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.