JavaScript变量运算符 [英] JavaScript Variable Operators

查看:109
本文介绍了JavaScript变量运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Javascript可以做到这些吗?

Are these possible in Javascript?

我有这样的东西:

var op1 = "<";
var op2 = ">";

if (x op1 xval && y op2 yval) {
 console.log('yay');
}

基本上,我需要用户输入操作员,该操作员来自选择框.

Basically I need the user to input the operator, its coming from a select box.

推荐答案

这是不可能的,但这是:

That is not possible, but this is:

var operators =
{
    '<': function(a, b) { return a < b; },
    '>': function(a, b) { return a > b; },
    /* ... etc. ... */
};

/* ... */

var op1 = '<';
var op2 = '>';
if (operators[op1](a, b) && operators[op2](c, d))
{
    /* ... */
}

这篇关于JavaScript变量运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆