Javascript中的嵌套函数

时间:2016-02-19 14:02:14

标签: javascript

我想有一个这样的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();

}

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

5 个答案:

答案 0 :(得分:2)

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    
        }
    }

})();

答案 1 :(得分:2)

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

activeField != null

答案 2 :(得分:1)

您要定义的内容称为对象文字。他们的语法如下:

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

然后你可以打电话

AudioRecording.method();

答案 3 :(得分:1)

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

你想创建一个对象文字,所以在摘要中它应该是这样的:

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();

所以你的代码看起来像这样:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        guard let text = acRegTextField.text else { return true }

        let newLength = text.utf16.count + string.utf16.count - range.length
        return newLength <= 4 // Bool
    }

答案 4 :(得分:0)

您可以使用松散扩充来应用模块模式,如下所示:

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模块的不同功能。

希望它有所帮助!