Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用继承引用基变量_Javascript_Jquery_Inheritance_Prototypal Inheritance - Fatal编程技术网

Javascript 如何使用继承引用基变量

Javascript 如何使用继承引用基变量,javascript,jquery,inheritance,prototypal-inheritance,Javascript,Jquery,Inheritance,Prototypal Inheritance,我一直在尝试许多方法来实现这一目标,但都没有成功。有人能帮我吗 基本对象: var Recorder = function (source) { this.context = source.context; var recording = null; this.start = function () { recording = true; } this.stop = function () { recording = f

我一直在尝试许多方法来实现这一目标,但都没有成功。有人能帮我吗

基本对象:

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

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

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

}
派生对象:

var messageRecorder = function () {

    this.isRecording = function () {
        return this.recording;
     }
}
所以我有一个基本对象,Recorder,它有一个变量'recording'。我想要一个扩展/派生对象messageRecorder,它可以返回'recording'的值(来自Recorder)。有什么建议吗?我尝试了jQuery扩展,var audioRecorder=$.extend({},new Recorder(),new messageRecorder()),但没有成功。我还尝试修改messageRecording,如下所示:

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

    this.isRecording = function () {
        return this.recording;      //declared in Recorder
     }
}
并像这样实例化:var audioRecorder=newmessagerecorder(source)


在我两次失败的尝试中,当我调用audioRecorder.start()时,它工作正常。调用audioRecorder.isRecording()时,变量“recording”未定义,可能是因为我没有尝试正确访问它。有什么建议吗?

您可以通过对正在创建的对象调用父构造函数来处理简单继承,因此可以执行以下操作:

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());
这将为您提供以下输出(正确设置
this.recording
):


您可以通过对正在创建的对象调用父构造函数来处理简单继承,因此可以执行以下操作:

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());
这将为您提供以下输出(正确设置
this.recording
):

录制
是一项重要功能。您绝对不能在
记录器
之外访问它。
录制
是一种功能。您绝对不能在
记录器
之外访问它。