0

I have been trying many ways to accomplish this without any success. Can anybody help me?

Base Object:

var Recorder = function (source) {
    this.context = source.context;
    var recording = null;

    this.start = function () {
        recording = true;
    }

    this.stop = function () {
        recording = false;
    }

}

Derived Object:

var messageRecorder = function () {

    this.isRecording = function () {
        return this.recording;
     }
}

So I have a base object, Recorder, which has a var 'recording'. I want to have an extended/derived object, messageRecorder, which can return the value of 'recording' (from Recorder). Any suggestions? I have tried the jQuery extend, var audioRecorder = $.extend( {}, new Recorder(), new messageRecorder()), with no luck. I have also tried to modify messageRecording as follows:

var messageRecorder = function () {
    Recorder.apple(this, arguments);

    this.isRecording = function () {
        return this.recording;      //declared in Recorder
     }
}

and instantiate like so: var audioRecorder = new messageRecorder(source);

In both my failed attempts, when I call audioRecorder.start() it works fine. When I call audioRecorder.isRecording(), the var 'recording' is undefined, probably because I am not trying to access it properly. Any suggestions?

tpdietz
  • 1,358
  • 9
  • 17
  • `recording` is a [variable, not an object property](http://stackoverflow.com/q/13418669/1048572). You cannot access it outside of `Recorder`, by no means. – Bergi Jul 02 '14 at 15:56

1 Answers1

4

You can handle simple inheritance by calling the parent constructor function on the object being created, so you can do the following:

var Recorder = function (source) {
    this.context = source.context;
    this.recording = null;

    this.start = function () {
        this.recording = true;
    };

    this.stop = function () {
        this.recording = false;
    };

};

var messageRecorder = function() {
    Recorder.apply(this, arguments);

    this.isRecording = function() {
        return this.recording;
    };
};

var src = {
    context: 'something'
};
var audioRecorder = new messageRecorder(src);

audioRecorder.start();
console.log("recording? " + audioRecorder.isRecording());
audioRecorder.stop();
console.log("recording? " + audioRecorder.isRecording());

Which gives you the following output (setting this.recording correctly):

recording? true
recording? false
David Atchley
  • 1,204
  • 8
  • 10