如果条件为分离函数

If condition on separated functions

本文关键字:函数 分离 条件 如果      更新时间:2023-09-26

下面是我喜欢做的一个例子:

var example = {
    before: function() {
        `if (1 == 1) {`
    },
    after: function() {
        `}`
    }
};
example.before();
alert('Success!');
example.after();

在这种情况下,我想用if (1 == 1)条件来涉及alert('Success!')

有可能吗?

试试这个

var example = {
    before: function() {
        return 'if (1 == 1) {';
    },
    after: function() {
        return '}';
    }
};
var code = example.before() + "alert('Success!');" + example.after();
eval(code);

但是,如果您正在编写一些常规的东西,那么您不应该在生产代码中像这样疯狂。