Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using function results in "function source code" and not evaluated function value?

Tags:

javascript

The following code sample (also at http://jsfiddle.net/MZwBS/)

var items = [];

items.push({
    example: function() {
        if(0 > 1) {
            return 'true';
        } else {
            return 'false';
        }
    }
});

document.write(items[0].example);

produces

'function () { if (0 > 1) { return "true"; } else { return "false"; } }'

instead of

'false'

It seems like I've been able something like this with ExtJS. Can anyone tell me where I went wrong? I'd like to evaluate anonymous functions like this on-the-fly.

like image 205
johnwp Avatar asked Mar 01 '26 13:03

johnwp


2 Answers

Do you mean to execute it?

document.write(items[0].example());​
like image 129
meder omuraliev Avatar answered Mar 03 '26 03:03

meder omuraliev


You want:

document.write(items[0].example());

When you skip the parentheses, you are saying, "Print this function." When you have them, you are saying, "Evaluate this function and print the result."

like image 30
Kendall Frey Avatar answered Mar 03 '26 02:03

Kendall Frey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!