对javascript来说,我是一个新问题。让我们考虑一个看起来像这样的字符串:
var str = "Math.random() > 0.5";
现在让我们使用一些javascript:
console.log( turnUp( str ) ); // true or false
因此,turnUp()只是我认为可以执行以下操作的函数:
没有turnUp():
console.log( str );
// Is equivalent to console.log( "Math.random() > 0.5" );
输出:
Math.random() > 0.5
使用turnUp():
console.log( turnUp( str ) );
// Is equivalent to console.log( Math.random() > 0.5 );
输出:
true
要么
输出:
false
因此,借助示例,您可能会了解我的需求!那么,如何制作turnUp()函数呢?
提前致谢
eval()有很多缺点,所以我希望您这样做:
var turnUp = function(str) {
return Function(' "use strict"; return (' + str + ') ')();
}
现在您可以:
var str = "Math.random() > 0.5";
console.log( str ); // => Math.random > 0.5
console.log( turnUp( str ) ); // => true or false
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句