0

I have a function in my object.

I want to access this function's variable from another function.Can anyone help me?

Here is my sample code. Any help would be greatly appreciated. Thanks.

var drops=  {
    hoverClass: "hoverme",
    greedy: true,
    accept: "#mini",
    drop: function(event,ui){
        var droppedOn2 = $(this);
        var dropped2 = $(ui.draggable);
        alert(droppedOn2.attr('id')); alert(dropped2.attr('id'));
        $(dropped2).appendTo(droppedOn2).draggable();

    },
    over: function(event, ui){
        console.log("buraya geliyorum > " + this.id);
    } 
};


$('#hebelek').sortable({
    connectWith: '#hebelek',
    cursor: 'pointer'
    }).droppable({
        accept: '#gridIcon1-2, #gridIcon2-2, #widgetIcon',
        activeClass: 'highlight',
        drop: function(event, ui) {
            var ilk = drops.droppedOn2;
            var droppedOn = $(this);
            var dropped = $(ui.draggable).clone();
            var fileName = dropped.attr('id');
            alert(droppedOn.attr('id')); alert(fileName);
            if (fileName == "gridIcon1-2"){   
               $(grid1_2Div).appendTo(droppedOn).find('.gridcell').droppable(drops)
            };

            if (fileName == "gridIcon2-2") {
               $(grid2_2Div).appendTo(droppedOn).find('.gridcell').droppable(drops)
            };

            if ((fileName == "widgetIcon") && (droppedOn.attr('id') == "hebelek")) {
               $(widgetDiv).appendTo("I WANT TO USE DROOPEDON2 ON HERE")
    };
    }

});  
linepisode
  • 65
  • 11
  • 1
    Which variable form which function? and which function do you want to access it from? provide more information! – musefan Nov 23 '12 at 09:41
  • I want to access "droppedOn2" (it is in drops object's drop function) from here : $(widgetDiv).appendTo("I WANT TO USE DROOPEDON2 ON HERE") – linepisode Nov 23 '12 at 09:50
  • Here is my example (jsfiddle.net/cT7Dk/17). I want to use it for selecting area which div is append after page load. Maybe somebody can give suggestion to solve this problem in other way. – linepisode Nov 23 '12 at 10:20

3 Answers3

1

Create a common scope for both functions (function wrapper will do), and create a variable in the same scope, like so:

(function () {
    var x = 5,
    f1 = function(){
        console.log(x);
    },
    f2 = function(){
        console.log(x);
    };

})();
Dziad Borowy
  • 12,368
  • 4
  • 41
  • 53
0

You cannot. Variables are scoped to the function in which they are declared. If you want to access a variable from two different functions you can declare it outside of both functions and access it from within them. For example:

var sharedVariable = 'something';
function a() {
     sharedVariable = 'a';
}
function b() {
     alert(sharedVariable); 
}
Max
  • 8,671
  • 4
  • 33
  • 46
0

To make a variable calculated in function A visible in function B, you have three choices:

  1. make it a global,
  2. make it an object property, or
  3. pass it as a parameter when calling B from A.
 

    function A()
    {
        var rand_num = calculate_random_number();
        B(rand_num);
    }

    function B(r)
    {
        use_rand_num(r);
    }

mim
  • 56
  • 1
  • 8