使用JavaScript变量作为函数名称? [英] Use JavaScript variable as function name?
问题描述
jQuery(document).ready(function(){
var actions = new Object();
var actions;
actions [0] ='create';
actions [1] ='update';
for(key in actions ){
//对话框
var actions [key] + Dialog = function(){
$('#'+ actions [key] +'dialog')。dialog('destroy' );
$('#'+ actions [key] +'dialog')。dialog({
resizable:false,
height:600,
width:400,
modal:true,
按钮:{
取消:function(){
$(this).dialog('close');
}
}
});
};
}
});
我想在循环中创建2个函数(createDialog和updateDialog)。我怎样才能做到这一点?在PHP中有非常简单的$$ var。但是如何让变量变量在JS中我不知道。
谢谢
actions [key +Dialog] = function(){...}
但是,由于Javascript函数通过引用捕获变量,您的代码将无法按预期工作。
您需要在一个单独的函数中定义内部函数,以便每个函数都获得一个单独的键
变量(或参数)。
例如:
var actionNames = ['create','update']; //这将创建一个包含两个元素的数组
var Dialog = {}; //这会为(var i = 0; i< actionNames.length; i ++){
对话框[actionNames [i]] = createAction(actionNames [i])创建一个空对象
。 );
函数createAction(key){
return function(){...};
$ / code>
您可以像这样使用它:
Dialog.create(...);
编辑
您试图污染具有多个对话框相关函数的全局名称空间。
这是一个糟糕的主意;最好把你的函数组织到命名空间中。
如果你真的想要关注全局命名空间,你可以这样做:
var actionNames = ['create','update']; //这为两个项目创建一个数组
for(var i = 0; i< actionNames.length; i ++){
this [actionNames [i] +'Dialog'] = createAction(actionNames [I]);
}
这会创建一个名为 createDialog
和
updateDialog
。
在一个普通的函数调用中, this
关键字指的是全局命名空间(通常是窗口
对象)。
I have the following code in Javascript:
jQuery(document).ready(function(){
var actions = new Object();
var actions;
actions[0] = 'create';
actions[1] = 'update';
for (key in actions) {
// Dialogs
var actions[key]+Dialog = function(){
$('#'+actions[key]+'dialog').dialog('destroy');
$('#'+actions[key]+'dialog').dialog({
resizable: false,
height:600,
width:400,
modal: true,
buttons: {
Cancel: function() {
$(this).dialog('close');
}
}
});
};
}
});
I want to create 2 functions in loop(createDialog and updateDialog). How can i do this? In PHP there is very simple $$var. But how make variable variable in JS I don't know.
Thank you
Like this:
actions[key + "Dialog"] = function () { ... };
However, since Javascript functions capture variables by reference, your code will not work as intended.
You need to define the inner function inside of a separate function so that each one gets a separate key
variable (or parameter).
For example:
var actionNames = [ 'create', 'update' ]; //This creates an array with two items
var Dialog = { }; //This creates an empty object
for (var i = 0; i < actionNames.length; i++) {
Dialog[actionNames[i]] = createAction(actionNames[i]);
}
function createAction(key) {
return function() { ... };
}
You can use it like this:
Dialog.create(...);
EDIT
You are trying to pollute the global namespace with multiple dialog-related functions.
This is a bad idea; it's better to organize your functions into namespace.
If you really want to polute the global namespace, you can do it like this:
var actionNames = [ 'create', 'update' ]; //This creates an array with two items
for (var i = 0; i < actionNames.length; i++) {
this[actionNames[i] + 'Dialog'] = createAction(actionNames[i]);
}
This will create to global functions called createDialog
and updateDialog
.
In a normal function call, the this
keyword refers to the global namespace (typically the window
object).
这篇关于使用JavaScript变量作为函数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!