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
5 Answers
Reset to default 4Because 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
5 Answers
Reset to default 4Because 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
版权声明:本文标题:telerik - javascript closure, not working? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744806345a2117783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论