将函数定义为对象参数
define a function as an object parameter
我正在开发一个firefox插件,而不是将整个脚本放在main.js中,而是尝试剪切不同的文件。
现在,我有一个utils.js,它看起来像这样:
'use strict';
module.exports.utils = function() {
return {
_ : require('sdk/l10n').get,
self : require('sdk/self'),
sStorage : require('sdk/simple-storage'),
tabs : require('sdk/tabs'),
pageMod : require('sdk/page-mod'),
toggleButton: require('sdk/ui/button/toggle'),
panel : require('sdk/panel'),
hotkey : require('sdk/hotkeys'),
displayPanel: function(mainPanel, qwantButton) {
if (mainPanel.isShowing) {
mainPanel.hide();
qwantButton.state('window', {
checked: false
});
} else {
mainPanel.show({
position: qwantButton
});
}
}
};
}();
我以这种方式将utils.js
导入main.js
:var utils = require(utils);
两个文件位于同一位置:[ROOT]/lib
。
但我遇到了一个错误,因为这一行:_ : require('sdk/l10n').get,
表示_不是函数。我试着这样写:_ : require('sdk/l10n').get(),
或get(str)
,但它们都失败了,第一次是因为get需要一个参数,第二次是因为没有定义str。有办法定义这个项目吗?
[编辑]:经过几次尝试,我发现以这种方式编写utils.js
可以使_
发挥作用:
'use strict';
var _ = require('sdk/l10n').get,
self = require('sdk/self'),
sStorage = require('sdk/simple-storage'),
tabs = require('sdk/tabs'),
{PageMode} = require('sdk/page-mod'),
{ToggleButton}= require('sdk/ui/button/toggle'),
{Panel} = require('sdk/panel'),
{Hotkey} = require('sdk/hotkeys');
var qwantButton = new ToggleButton({
id: 'toolbar_button_id',
label: _('toolbar_button_label'),
icon: {
'16': './img/logo-16.png',
'32': './img/logo-32.png',
'64': './img/logo-64.png'
},
onClick: displayMainPanel
});
var mainPanel = new Panel({
width: 525,
height: 175,
contentURL: self.data.url('./panel.html'),
contentScriptFile: self.data.url('./js/panelScript.js')
});
var hotkey = new Hotkey({
combo: 'alt-Q',
onPress: displayMainPanel(qwantButton, mainPanel)
});
function displayMainPanel(mainPanel, qwantButton) {
if (mainPanel.isShowing) {
mainPanel.hide();
qwantButton.state('window', {
checked: false
});
} else {
mainPanel.show({
position: qwantButton
});
}
}
module.exports = function utils() {
return {
_ : _,
self : self,
sStorage : sStorage,
tabs : tabs,
hotkey : hotkey,
qwantButton : qwantButton,
mainPanel : mainPanel,
displayMainPanel: displayMainPanel
};
}();
但现在,所需元素的方法不起作用(例如mainPanel.show()
)。。。
我拿到了。
根据其名称,main.js
是主要元素,因此任何在外部完成的操作都必须在内部完成。对于SDK的功能,我这样做了:
main.js
:
'use strict';
var Core = function() {
var _ = require('sdk/l10n'),
self = require('sdk/self'),
sPrefs = require('sdk/simple-prefs'),
sStorage = require('sdk/simple-storage'),
tabs = require('sdk/tabs'),
hotkey = require('sdk/hotkeys'),
pageMod = require('sdk/page-mod'),
panel = require('sdk/panel'),
toggleButton= require('sdk/ui/button/toggle');
return {
_ : _,
self : self,
sPrefs : sPrefs,
sStorage : sStorage,
tabs : tabs,
hotkey : hotkey,
pageMod : pageMod,
panel : panel,
toggleButton : toggleButton
};
};
module.exports = Core;
var Interface = require('interface');
var qwantInterface = new Interface();
因此,整个SDK被封装在Core
函数中,我将其导出。现在,每个其他文件都需要main.js
并获得Core。例如,the interface.js
:
'use strict';
var Core = require('main');
var core = new Core();
var Interface = function() {
var qButton = core.toggleButton.ToggleButton({
id: 'toolbar_button_id',
label: core._.get('toolbar_button_label'),
icon: {
'16': './img/logo-16.png',
'32': './img/logo-32.png',
'64': './img/logo-64.png'
},
onClick: displayPanel
}),
qPopup = core.panel.Panel({
width: 525,
height: 175,
contentURL: core.self.data.url('popup.html'),
contentScriptFile: core.self.data.url('./js/popupScript.js')
}),
qHotkey = core.hotkey.Hotkey({
combo: 'alt-Q',
onPress: displayPanel
});
function displayPanel() {
if (qPopup.isShowing) {
qPopup.hide();
qButton.state('window', {
checked: false
});
} else {
qPopup.show({
position: qButton
});
}
}
};
module.exports = Interface;
现在,导出的Interface
是必需的,并按预期在main.js
的底部执行。
相关文章:
- JS for循环来设置HTML对象参数
- 如何在使用javascript加载页面时直接设置对象参数
- 如何将对象参数传递给事件处理程序
- 在ng重复中剪切对象参数的字符串长度
- 在新对象参数中使用“this”选择器
- 在 javascript 中通过引用传递一个对象参数
- 为什么 Javascript 中的参数在使用 console.log 时返回 [对象参数]
- 如何使用 javascript 访问在函数中传递的对象参数
- 在哪里可以获得有关 JavaScript 函数的对象参数语法的信息
- JavaScript 对象参数在通过网络读取时更改
- 我正在寻找一种智能方法来检查传递给函数的对象参数
- 使用来自另一个函数的对象参数调用函数
- Javascript中的对象参数,以及我们应该如何命名它们
- Jquery 在 Firefox 中的自定义对象参数(动态导航加载动态内容)
- 有效记忆对象参数
- AngularJS v1.2.x $digest() 无限循环指令隔离范围对象参数
- 在 Jasmine 中执行有关对象参数的断言
- Jquery - 编辑对象参数
- 检查对象参数是否具有所有必需属性的最有效方法是什么
- Javascript对象参数的分配和使用