微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JavaScript OOP - 函数调用继承不起作用

如何解决JavaScript OOP - 函数调用继承不起作用

我有 2 个文件FPDevice.jsFPComponent.js)。每个文件都有一个对象。我需要从一个文件(Object)调用一个函数到另一个文件(Object)。

文件 FPComponent.prototype.onSysexEvent.call(FPComponent); 文件上使用 FPDevice.js 可以正常工作,但没有达到预期效果。我期望通过在上下文参数中使用 FPComponent 而不是 this,该函数将继承 FPComponent 对象的属性并能够使用 this.channels。>

  1. 当从 FPComponent.prototype.onSysexEvent.call(FPComponent); 函数调用 FPMidiDevice.prototype.onSysexEvent 函数时,调用有效但继承无效。

  2. 出现错误 - this.channels is undefined

  3. 由于 this.channelsFPComponent 对象的属性,所以我需要调用 FPComponent.prototype.onSysexEvent.call(FPComponent) 的继承来自 FPComponent 对象,以便我可以使用this.channels 中的 FPComponent.prototype.onSysexEvent 属性

  1. 我正在寻找的结果是,一旦从 FPComponent.prototype.onSysexEvent 函数调用函数 FPComponent 就会从 FPMidiDevice.prototype.onSysexEvent 对象继承属性

FPDevice.js

//FPDevice.js

var __extends = (this && this.__extends) || (function () 
{
    var extendStatics = function (d,b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d,b) { d.__proto__ = b; }) ||
            function (d,b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b,p)) d[p] = b[p]; };
        return extendStatics(d,b);
    };
    return function (d,b) {
        extendStatics(d,b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype,new __());
    };
})();

include_file("resource://com.presonus.musicdevices/sdk/controlsurfacecomponent.js");
include_file("resource://com.presonus.musicdevices/sdk/controlsurfacedevice.js");
include_file("FPComponent.js");

var FPMidiDevice = (function (_super) 
{
    __extends(FPMidiDevice,_super);    
    
    function FPMidiDevice(faderCount,sysexHeader) 
    {       
        var _this = _super.call(this) || this;
        _this.faderCount = faderCount;
        _this.sysexHeader = sysexHeader;
        _this.lastActiveSensing = 0;
        _this.activeSensingInterval = 2500;             
        return _this;
    }
    
    FPMidiDevice.prototype.onInit = function (hostDevice) 
    {
        _super.prototype.onInit.call(this,hostDevice);
        this.meters = [];              
    };  
    
         FPMidiDevice.prototype.onSysexEvent = function (data,length) 
     {                  
        //##########################################################################################################    //##########################################################################################################
        // I am trying to use call but using FPComponent object so the call can inherit the properties from FPComponent         
        
         FPComponent.prototype.onSysexEvent.call(FPComponent);                       
     };  
    
    
    return FPMidiDevice;
}(PreSonus.ControlSurfaceDevice));

function createFP16DeviceInstance()
 {
    return new FPMidiDevice(16,FP.Support.kSysexHeaderFP16);
}

FPComponent.js

//FPComponent.js

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d,new __());
    };
})();

include_file("resource://com.presonus.musicdevices/sdk/controlsurfacecomponent.js");
include_file("resource://com.presonus.musicdevices/sdk/controlsurfacedevice.js");

var FPComponent = (function (_super) 
{   
    __extends(FPComponent,_super);

    function FPComponent(faderCount) 
    {
        var _this = _super.call(this) || this;
        _this.faderCount = faderCount;
        _this.pagingStatusIndex = faderCount - 1;
        _this.channels = [];
        return _this;
    }       

 FPComponent.prototype.onInit = function (hostComponent) 
    {
        
        PreSonus.ControlSurfaceComponent.prototype.onInit.call(this,hostComponent);            
       
        this.model = hostComponent.model;
        this.root = this.model.root;
        this.mixerMapping = this.root.find("mixerMapping");
        this.channelBankElement = this.mixerMapping.find("ChannelBankElement");        
        var paramList = hostComponent.paramList;
                
        for (var i = 0; i < this.faderCount; i++) 
        {
            var channel = new ChannelStrip;                 
            channel.channelElement = this.channelBankElement.getElement(i);                 
            channel.textValue = paramList.addalias("textValue" + i);
            channel.panValue = paramList.addalias("panValue" + i);
            channel.muteValue = paramList.addalias("muteValue" + i);
            channel.solovalue = paramList.addalias("solovalue" + i);
            channel.recordValue = paramList.addalias("recordValue" + i);
            channel.monitorValue = paramList.addalias("monitorValue" + i);
            channel.colorValue = paramList.addalias("colorValue" + i);            
            
            this.channels.push(channel);                    

        }
    };  
    
    //########################################################################################################
    // it is not working properly when called from FPMidiDevice.prototype.onSysexEvent on file FPMidiDevice.js  
    // error = this.channels is undefined
    
    FPComponent.prototype.onSysexEvent = function () 
     {          
        var  channelIndex = 0;  
        var channel =  this.channels[channelIndex];
        channel.faderValue.value = 1;       
        
      };          
      
          return FPComponent;
           
}(PreSonus.ControlSurfaceComponent));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。