2016-02-19 139 views
1

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

} 

我很感激您可以提供的任何帮助。谢谢!

+3

您没有有效的javascript在这里! –

+0

你应该参考以下文章[MDN - 面向对象的JavaScript简介](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript) – Rajesh

+0

您可能正在寻找一个[Object面向对象的JS教程](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript)... – dgiugg

回答

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

})(); 
+0

谢谢!这对我来说是最容易实现的,它的作用就像一个魅力。 –

1

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

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

然后你就可以调用

AudioRecording.method(); 
2

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

}; 
1

这是可能的,如果您遵守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(); 
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模块的异步不同功能。

¡希望它有帮助!