Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

js需要生成环境执行表达式 ('a-b'),有什么好办法?

需要生成环境执行表达式有什么好办法

现实需求

已实现如下

 () => {
        const env_params = 'a, b'
        const env_args = [1, 2]
        const condition = 'a === b'
        const command = `function(${env_params}){ return ${condition}}`;
        return Function(`"use strict";return ${command}`)().apply(null, env_args);
    }

如下图 分输条件类型(DeliverType)为xxx时,分输量(m3/h)(不可编辑)

图片.png
数据是 Condition: "DeliverType=='xxxx'"
DeliverType我要每行求当前行的分输条件,并执行表达式进行判断


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var scriptElement = document.createElement('script');
scriptElement.setAttribute('id','____tmp_condition_script_____');
scriptElement.innerText="function test(){console.log('~~gogo~~');return true;}";//传入自定义函数
var header = document.querySelector('head');
header.appendChild(scriptElement);

var conditionResult = test();//执行自定义函数
header.removeChild(scriptElement);//删除script元素
test = null;//删除test全局函数

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...