Javascript中的嵌套函数

Nested functions in Javascript

本文关键字:函数 嵌套 Javascript      更新时间:2023-09-26

我想要一个这样的JavaScript块,但这似乎无效。

MYAPP.audioRecording = {
  var navigator = window.navigator;
  var Context = window.AudioContext || window.webkitAudioContext;
  var context = new Context();
  navigator.getUserMedia = (
    navigator.getUserMedia ||
    navigator.webkitGetUserMedia ||
    navigator.mozGetUserMedia ||
    navigator.msGetUserMedia
  );
  function startRecorder() {
     //start recording code
  }
  function stopRecorder() {
     //recorder stop    
  }
}

然后我想从另一个代码块调用 startRecorder() 和 stopRecorder() 函数,就像这样。

MYAPP.recordingManager = {
  MYAPP.audioRecording.startRecorder();
  MYAPP.audioRecording.stopRecorder();
}

感谢您提供的任何帮助。谢谢!

MYAPP.audioRecording = (function() {
    var navigator = window.navigator;
    var Context = window.AudioContext || window.webkitAudioContext;
    var context = new Context();
    navigator.getUserMedia = (
        navigator.getUserMedia ||
        navigator.webkitGetUserMedia ||
        navigator.mozGetUserMedia ||
        navigator.msGetUserMedia
    );
    return {
        startRecorder: function() {
            //start recording code
        },
        stopRecorder: function() {
            //recorder stop    
        }
    }
})();

在Javascript中,通过对象文字创建对象仅允许将函数定义为对象方法。更改代码,如下所示:

var MYAPP = MYAPP || {};
MYAPP.audioRecording = {
  navigator : window.navigator,
  Context : window.AudioContext || window.webkitAudioContext,
  getUserMedia: (
    this.navigator.getUserMedia ||
    this.navigator.webkitGetUserMedia ||
    this.navigator.mozGetUserMedia ||
    this.navigator.msGetUserMedia
  ),
  startRecorder : function () {
     //start recording code
  },
  stopRecorder : function() {
     //recorder stop    
  }
};

您尝试定义的称为对象文字。它们的语法如下:

AudioRecording = {
    property: 'SomeProp',
    method: function() {
       console.log(this.property); // Will log 'SomeProp'
    }
}

然后你可以打电话

AudioRecording.method();

如果您遵守 JSON 的规则,这是可能的。

你想要创建一个对象文字,所以抽象起来,它应该看起来像这样:

myNameSpace = {
  STATICPROPERTY: 'test',
  method: function() {
    var variable = 'thisVarIsPrivateToMethod';
    this.variable = 'dynamic';
  },
  init: function() {
    this.method();
    console.log(this.STATICPROPERTY);  // will log "test"
    console.log(this.variable);        // will log "dynamic"
  }
}
myNameSpace.init(); 

因此,您的代码可能如下所示:

MYAPP.audioRecording = {
  startRecorder: function() {
    //start recording code
    var myContext = this.context,
        myMedia = this.getUserMedia;
  }
    stopRecorder: function() {
    //recorder stop
  },
  init: function() {
    var Navigator = window.navigator,
        Context = window.AudioContext || window.webkitAudioContext;
    Navigator.getUserMedia = (
      Navigator.getUserMedia ||
      Navigator.webkitGetUserMedia ||
      Navigator.mozGetUserMedia ||
      Navigator.msGetUserMedia
    );
    this.context = new Context();
    this.getUserMedia = Navigator.getUserMedia;
    return this;
  }
}
MYAPP.audioRecording.init().startRecorder();

您可以像这样应用带有松散增强的模块模式:

var MYAPP = (function(appModule) {
    // Here you are augmenting your existing MYAPP object 
    // adding a new object returned by this IIFE
    appModule.audioRecording = (function() {
        navigator.getUserMedia = (
            navigator.getUserMedia ||
                navigator.webkitGetUserMedia ||
                navigator.mozGetUserMedia ||
                navigator.msGetUserMedia);
        return {
            startRecorder: function() {
                //start recording code
            },
            stopRecorder: function() {
                //recorder stop    
            }
        };
    })();
    return appModule;
// Notice how the main IIFE is receiving the already defined MYAPP
// object. If this object hasn't been defined, it passes an empty object literal
})(MYAPP || {}); 

这是一种非常合适的模式,用于扩充已定义的现有模块,允许您异步加载同一 MYAPP 模块的不同功能。

○希望对您有所帮助!