简体   繁体   English

javascript返回值绑定为对象值的函数值

[英]javascript return value of function binded as object value

I can not return values bools / strings using the following convention. 我不能使用以下约定返回值bools / strings。

http://jsfiddle.net/eQWyY/ http://jsfiddle.net/eQWyY/

<input id="lol" type="text" value="ahh" />

<script>
function test() {
    var flag = false;
    if ($('#lol').val()) {
        alert($('#lol').val()); //"sanity check - pass"
        flag = true;
    }
    return flag;
};

function modal() {};
var m = new modal();

modal.prototype.init = function(x) {
    this.input = x;
    this.modals = {};
    this.initModalFrames();
    this.executeLogic();
}

modal.prototype.initModalFrames = function() {
    for (i in this.input) {
        this.modals[i] = {
            handle :    this.input[i].handle,
            frame :     this.input[i].frame,
            logic :     this.input[i].logic ? this.input[i].logic : null
        };
    }
}

modal.prototype.executeLogic= function() {
    var s = this;
    $.each(this.modals, function(i, o) {
        if(s.modals[i].logic != null) {
            var x = s.modals[i].logic.call();
            if(x) {
                alert('true');
            }
            else {
                //Always evaluates to false
                alert('false');
            }
        }
    });
}

m.init([
      {handle: '#handle1', frame: '#frame2', logic: function() {test();}},
]);

</script>

The function does not seem to return anything and always evaluates to false. 该函数似乎没有返回任何内容,并且总是计算为false。 What am I doing wrong here? 我在这做错了什么?

Your logic handler should return the result of test execution: 您的logic处理程序应返回test执行的结果:

{ handle: '#handle1', frame: '#frame2', logic: function() { return test(); } }

or simpler: 或者更简单:

{ handle: '#handle1', frame: '#frame2', logic: test }

DEMO: http://jsfiddle.net/eQWyY/1/ 演示: http //jsfiddle.net/eQWyY/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM