Javascript中的嵌套函数
Nested functions in 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();
}
感谢您提供的任何帮助。谢谢!
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 模块的不同功能。
○希望对您有所帮助!
相关文章:
- 用嵌套函数和默认函数定义函数
- d3中堆栈函数和嵌套函数之间的差异
- JavaScript 中的嵌套函数和 “this” 关键字
- 嵌套到另一个函数中的Fancybox函数;不起作用
- Javascript中带有返回值的嵌套函数
- 访问嵌套函数结构中的JavaScript父函数变量
- 从嵌套函数访问函数属性
- 如何在javascript中使用嵌套函数作为生成器(使用“inner”yields)
- Javascript嵌套函数属性继承
- 具有嵌套对象数组的 Javascript 对象的递归搜索函数
- Facebook Javascript 函数嵌套循环
- 将动态 AJAX 函数嵌套在 JavaScript 函数中 - 并保持更新
- 对由多个groupBy函数嵌套的对象数组进行分组
- 从父函数返回什么以获取从子函数/嵌套函数返回的值
- jQuery匿名函数-嵌套函数
- ajax调用函数嵌套
- 函数嵌套括号的Python风格指南是什么?
- setTimeout函数中的关键字,该函数嵌套在on()方法下.但这并没有奏效
- 嵌套异步函数嵌套同步函数
- 如何克服函数嵌套