javascript - How to write a jQueryJS function so that it returns a value? - Stack Overflow

How do I write a jQuery function so that it returns a value that I can access outside the function? I d

How do I write a jQuery function so that it returns a value that I can access outside the function? I did something like this but when I tried to access it outside the function, it says "undefined":

$(document).ready(function(){
    function add(y, z) {
        $('p').click(function() {
            x = y + z;
            console.log(x); // shows 3
            return x;
        });     
    }

    var sum = add(1, 2);

    console.log(sum); // shows "undefined"
});

How do I write a jQuery function so that it returns a value that I can access outside the function? I did something like this but when I tried to access it outside the function, it says "undefined":

$(document).ready(function(){
    function add(y, z) {
        $('p').click(function() {
            x = y + z;
            console.log(x); // shows 3
            return x;
        });     
    }

    var sum = add(1, 2);

    console.log(sum); // shows "undefined"
});
Share Improve this question asked Sep 30, 2011 at 14:30 catandmousecatandmouse 11.8k24 gold badges95 silver badges158 bronze badges 2
  • why do you have the click event within the function? Also, you do not necessarily need to declare functions within the ready() method. – Tim B James Commented Sep 30, 2011 at 14:39
  • I think I'll create another question for that now that I know that the click event is the problem. For now this question is resolved. – catandmouse Commented Sep 30, 2011 at 14:45
Add a ment  | 

5 Answers 5

Reset to default 4

This should work. Not sure why you added the click event there

$(document).ready(function(){
    function add(y, z) {
        x = y + z;
        console.log(x); // shows 3
        return x;
    }
    var sum = add(1, 2);

    console.log(sum); 
});
function add(y, z) {
   $('p').click(function() {
        x = y + z;
        console.log(x); // shows 3
        return x;
    });   
    return y + z;
}

Simple answer - you can't.

The method registered by .click() is called asynchronously as part of the Javascript event loop.

Your add function has no return value at all.

If instead what you're trying to do is to create a jQuery style utility function, rather than a method that acts on elements, you would do this:

(function($) {
     $.add = function(y, z) {
         return y + z;
     }
})(jQuery);

usage:

var x = $.add(1, 2);

The function, in that case, is an anonymous callback function that is called when the click event occurs. The way you set it up, what function add(x, y) does is create a new handler for click events on

tags.

I don't know why click event is added to the function. It should be handled separately. The code should look like this which makes sense according to me.

$(document).ready(function() {
function add(y, z) {
    var x = y + z; // good to make it local scope
    console.log(x); // shows 3
    return x;
}
var sum = add(1, 2);
document.write(sum); // shows "undefined"
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744781474a4593368.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信