如何从动态属性调用和 Uncaught ReferenceError 中删除 eval()
我正在尝试动态调用对象属性。我已经用 eval() 解决了这个问题,但我知道 eval 是邪恶的,我想以更好、更安全的方式做到这一点。我的评估代码:
// onfocus
var classes = this.getAttribute('class').split(' ');
for(var i = 0; i < classes.length; ++i) {
if(classes[i].match(/val\- */) !== null) {
var rule = classes[i].substr(4);
var instruction = eval('validate.instructionTexts.'+ rule +'()');
tooltip.appendChild( document.createTextNode(instruction) );
}
}
我也有这个代码:
// onblur
var classes = this.getAttribute('class').split(' ');
for( var i = 0; i < classes.length; ++i ){
if(classes[i].match(/val\- */) !== null) {
var rule = classes[ i ].substr( 4 );
var tooltip = document.getElementsByClassName( 'tooltip' );
for( i = 0; i < tooltip.length; ++i){
tooltip[ i ].style.display = 'none';
}
eval('validate.rules.'+ rule +'(' + (this.value) + ')');
}
第二个代码的问题是我想向我的属性发送一个字符串。 this.value = 我在文本框中输入的文本,因此我从 this.value 中获得了正确的字符串,但出现了此错误。
如果我输入 foo. 未捕获的引用错误:foo 未定义。 Javascript 认为我试图发送一个变量,但我希望它发送一个字符串。我该如何解决这个问题?
I'm trying to call object properties dynamically. I have solved the problem with eval() but i know that eval is evil and i want to do this on a better and safer way. My eval code:
// onfocus
var classes = this.getAttribute('class').split(' ');
for(var i = 0; i < classes.length; ++i) {
if(classes[i].match(/val\- */) !== null) {
var rule = classes[i].substr(4);
var instruction = eval('validate.instructionTexts.'+ rule +'()');
tooltip.appendChild( document.createTextNode(instruction) );
}
}
And I also have this code:
// onblur
var classes = this.getAttribute('class').split(' ');
for( var i = 0; i < classes.length; ++i ){
if(classes[i].match(/val\- */) !== null) {
var rule = classes[ i ].substr( 4 );
var tooltip = document.getElementsByClassName( 'tooltip' );
for( i = 0; i < tooltip.length; ++i){
tooltip[ i ].style.display = 'none';
}
eval('validate.rules.'+ rule +'(' + (this.value) + ')');
}
the problem with the second code is that I want to send a string to my property. this.value = the text i type in my textbox so i get correct string from this.value but i got this error.
if i type foo.
Uncaught ReferenceError: foo is not defined. Javascript thinks I trying to send a variabel but i want it to send a string. How can i solve this problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
object.property
或通过方括号符号object['property']
访问。/val\- */
匹配字符串中任意位置的字符 v、a、l、“-”连字符以及零个或多个空格。val-
开头的类,那么indexOf
方法更容易阅读,而且可能也更高效。我已经相应地调整了你的代码。我假设验证规则的类名均以
val-
开头,类名的其余部分是规则的名称:object.property
or via the square-bracket-notationobject['property']
./val\- */
matches the characters v, a, l, a '-' hyphen, and zero or more spaces, anywhere in the string.val-
, then theindexOf
method is much easier to read, and probably also a lot more efficient.I've adjusted your bits of code accordingly. I'm assuming that the class names for your validation rules all start with
val-
, and that the rest of the class name is the name for the rule:您不需要使用 eval,您可以通过以下方式访问它:
这也将解决您的其他问题,即您传入
this.value
的值,当 eval()'d 时没有被引用为字符串('foo' 是),因此被解释为变量。You do not need to use eval, you can access it as:
Which will solve your other problem too, which is that you are passing in the value of
this.value
which when eval()'d is not quoted as a string (which 'foo' is) so is being interpreted as a variable.要从对象 obj 获取属性 foo,您可以使用
或
第一个不允许保留字或者属性包含空格。
所以你的第一个例子可以改为
to get a property foo from object obj, you could use either
or
The first one won't allow reserved words or if the property contains spaces.
So your first example could change to