[英]How can I integrate this for loop into this function
Trying to add a little customisation to a plugin. 尝试为插件添加一些自定义。
I have the following loop: 我有以下循环:
for (var i = 0; i < key_name.Length; i++) {
a.extend(a.ZeEditor.DEFAULTS.itemKeys,
{[key_name[i]]: "font-size: 15px; color: black;"});
}
I need to run it in this function (or else 'a' won't be defined) 我需要在此函数中运行它(否则将不会定义“ a”)
function(a) {
"use strict";
a.extend(a.ZeEditor.DEFAULTS, {
itemKeys: {
[key_name[0]]: "font-size: 15px; color: black;"
}
}),
a.ZeEditor.PLUGINS.itemKey = function(b) {...},
a.ZeEditor.RegisterCommand("itemKey", {...}),
a.ZeEditor.DefineIcon("itemKey", {...})
});
No matter where I put it the 2 most common out comes are either nothing happens (so the coding isn't executed for whatever reason) or I get the error Uncaught SyntaxError: Unexpected token for
. 无论我把它放在哪里,最常见的2个都是什么都没有发生(因此无论出于何种原因都不会执行编码),或者我收到错误信息Uncaught SyntaxError: Unexpected token for
。
I'm thinking I need to add a new function to this coding and put it in there but I cant even find a spot to put the function without an error. 我在想我需要为此编码添加一个新函数并将其放在其中,但我什至找不到一个可以正确放置函数的位置。 or nothing happening eg 或什么也没有发生
...
a.ZeEditor.PLUGINS.itemKey = function(b) {...},
a.ZeEditor.RegisterCommand("itemKey", {...}),
a.ZeEditor.DefineIcon("itemKey", {...}),
function KeysLoop() {
for (var i = 0; i < key_name.Length; i++) {
a.extend(a.ZeEditor.DEFAULTS.itemKeys, {[key_name[i]]: "font-size: 15px; color: black;"});
}
}
});
This doesn't trow an error but also doesn't loop through the array 这不会引发错误,但也不会遍历数组
I'm still learning javascript and this is a bit beyond my knowledge so I need some help. 我仍在学习javascript,这超出了我的知识范围,因此我需要一些帮助。
EDIT 编辑
Sorry I left some stuff out 对不起,我遗漏了一些东西
key_name is just a simple array key_name = ["text1", "text2", "text3",]
key_name只是一个简单的数组key_name = ["text1", "text2", "text3",]
The loop is so I don't have to manually put all the array values as the objects option eg 循环是这样,所以我不必手动将所有数组值都作为对象选项,例如
a.extend(a.ZeEditor.DEFAULTS, {
itemKeys: {
"text1": "font-size: 15px; color: black;",
"text2": "font-size: 15px; color: black;",
}
Also I don't know the array values or how many there are so I need to loop through the array. 我也不知道数组的值或有多少,所以我需要遍历数组。
This line that is in the for loop works fine when put at the bottom of the function so I know the line works 当放在函数底部时,for循环中的这一行工作正常,因此我知道该行有效
...
a.ZeEditor.PLUGINS.itemKey = function(b) {...},
a.ZeEditor.RegisterCommand("itemKey", {...}),
a.ZeEditor.DefineIcon("itemKey", {...}),
a.extend(a.ZeEditor.DEFAULTS.itemKeys, {[key_name[1]]: "font-size: 15px; color: black;"});
// this line ^^
});
Define function KeysLoop()
outside function(a){}
, and then call KeysLoop(). 在function(a){}
之外定义function KeysLoop()
,然后调用KeysLoop()。 Because you are declaring your function but never invoking it. 因为您在声明函数但从未调用它。
function KeysLoop(a) {
for (var i = 0; i < key_name.Length; i++) {
a.extend(a.ZeEditor.DEFAULTS.itemKeys, {[key_name[i]]: "font-size: 15px; color: black;"});
}
}
...
a.ZeEditor.PLUGINS.itemKey = function(b) {...},
a.ZeEditor.RegisterCommand("itemKey", {...}),
a.ZeEditor.DefineIcon("itemKey", {...}),
KeysLoop(a);
});
PD: function names by convention are in camelCase and start with lower case eg: keysLoop() PD:函数名称按惯例位于camelCase中,并以小写字母开头,例如:keysLoop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.