admin管理员组

文章数量:1024592

I have a function that I call to retrieve a sliding pane (telerik splitter control) I thought I could use this function

function getZone() {
        var slidingZone = $find("<%= slidingZone.ClientID %>");
        return function () { return slidingZone; };
    }

so that it didn't have to "find" the sliding zone every time. But it doesn't work.

This does...

function getZone() {
        var slidingZone = $find("<%= slidingZone.ClientID %>");
        return slidingZone;    
}

Can you tell me why the first one isn't working?

BTW, I'm using it like this....

function hideTreePane() {
        var paneId = "<%= slidingPane.ClientID %>";
        getZone().undockPane(paneId);
        return true;
    }

I have a function that I call to retrieve a sliding pane (telerik splitter control) I thought I could use this function

function getZone() {
        var slidingZone = $find("<%= slidingZone.ClientID %>");
        return function () { return slidingZone; };
    }

so that it didn't have to "find" the sliding zone every time. But it doesn't work.

This does...

function getZone() {
        var slidingZone = $find("<%= slidingZone.ClientID %>");
        return slidingZone;    
}

Can you tell me why the first one isn't working?

BTW, I'm using it like this....

function hideTreePane() {
        var paneId = "<%= slidingPane.ClientID %>";
        getZone().undockPane(paneId);
        return true;
    }
Share Improve this question asked Nov 15, 2010 at 15:03 Tom BTom B 2,1802 gold badges13 silver badges15 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

Because your returning a function and you need to evaluate it... If using the first function this may work..

function hideTreePane() { 
    var paneId = "<%= slidingPane.ClientID %>"; 
    var zoneFunc = getZone();
    zoneFunc().undockPane(paneId); 
    return true; 
} 

You will need to call the function you are returning from getZone:

getZone()().undockPane( paneId );

It wasn't working because the function getZone itself does not have a member called undockPane.

EDIT:

I think it would be better to do this:

function getZone() {
    if ( getZone.cache === undefined )
        getZone.cach = $find("<%= slidingZone.ClientID %>");
    return getZone.cache;
}

Then you would call like this:

getZone().undockPane( paneId );

Well, the first function, returns a function, and you want the inner function to be executed.

If you invoke the result of it you will see it work, e.g.:

getZone()();

I think you want the following, use an immediately executed anonymous function, to call the $find method only once, storing its result:

var getZone = (function() {
  var slidingZone = $find("<%= slidingZone.ClientID %>");
  return function () { return slidingZone; };
})();

In your first example you're returning a function, therefore getZone() bees a function itself and you need to do getZone()() to get the slidingZone value you want.

There's no need to wrap your return value in a function for this case.

You're returning a function from the first example, not a value. You'd need to evaluate the function for it to work. Try something like.

var slidingZone;
function getZone() {
    if (!slidingZone) {
        slidingZone = $find( ... );
    }
    return slidingZone;
}

It would be better for this to be part of a "class" so that the caching variable isn't in the global scope.

I have a function that I call to retrieve a sliding pane (telerik splitter control) I thought I could use this function

function getZone() {
        var slidingZone = $find("<%= slidingZone.ClientID %>");
        return function () { return slidingZone; };
    }

so that it didn't have to "find" the sliding zone every time. But it doesn't work.

This does...

function getZone() {
        var slidingZone = $find("<%= slidingZone.ClientID %>");
        return slidingZone;    
}

Can you tell me why the first one isn't working?

BTW, I'm using it like this....

function hideTreePane() {
        var paneId = "<%= slidingPane.ClientID %>";
        getZone().undockPane(paneId);
        return true;
    }

I have a function that I call to retrieve a sliding pane (telerik splitter control) I thought I could use this function

function getZone() {
        var slidingZone = $find("<%= slidingZone.ClientID %>");
        return function () { return slidingZone; };
    }

so that it didn't have to "find" the sliding zone every time. But it doesn't work.

This does...

function getZone() {
        var slidingZone = $find("<%= slidingZone.ClientID %>");
        return slidingZone;    
}

Can you tell me why the first one isn't working?

BTW, I'm using it like this....

function hideTreePane() {
        var paneId = "<%= slidingPane.ClientID %>";
        getZone().undockPane(paneId);
        return true;
    }
Share Improve this question asked Nov 15, 2010 at 15:03 Tom BTom B 2,1802 gold badges13 silver badges15 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

Because your returning a function and you need to evaluate it... If using the first function this may work..

function hideTreePane() { 
    var paneId = "<%= slidingPane.ClientID %>"; 
    var zoneFunc = getZone();
    zoneFunc().undockPane(paneId); 
    return true; 
} 

You will need to call the function you are returning from getZone:

getZone()().undockPane( paneId );

It wasn't working because the function getZone itself does not have a member called undockPane.

EDIT:

I think it would be better to do this:

function getZone() {
    if ( getZone.cache === undefined )
        getZone.cach = $find("<%= slidingZone.ClientID %>");
    return getZone.cache;
}

Then you would call like this:

getZone().undockPane( paneId );

Well, the first function, returns a function, and you want the inner function to be executed.

If you invoke the result of it you will see it work, e.g.:

getZone()();

I think you want the following, use an immediately executed anonymous function, to call the $find method only once, storing its result:

var getZone = (function() {
  var slidingZone = $find("<%= slidingZone.ClientID %>");
  return function () { return slidingZone; };
})();

In your first example you're returning a function, therefore getZone() bees a function itself and you need to do getZone()() to get the slidingZone value you want.

There's no need to wrap your return value in a function for this case.

You're returning a function from the first example, not a value. You'd need to evaluate the function for it to work. Try something like.

var slidingZone;
function getZone() {
    if (!slidingZone) {
        slidingZone = $find( ... );
    }
    return slidingZone;
}

It would be better for this to be part of a "class" so that the caching variable isn't in the global scope.

本文标签: telerikjavascript closurenot workingStack Overflow