所以我知道我可以调用这样的函数:
var groupSetter = function(){
console.log('asdfasdf');
};
//I want to remove this next line
groupSetter();
并从中回忆起来:
$( window ).resize(function() {
// this call is fine
groupSetter();
});
我知道这看起来很傻但更像是这样。
(function(){
var groupSetter = function(){
console.log('asdfasdf');
};
})();
是否可以使var groupSetter成为匿名函数,但仍然可以在窗口调整大小时调用它?
更新
var groupSetter = (function(){
var currentPage = Carousel.pageData[scope.page.pageindex - 1];
scope.containerStyles = {
width: currentPage.size.updated.width + 'px',
height: currentPage.size.updated.height + 'px'
};
})();
angular.element($window)
.on('resize', function (e) {
groupSetter;
});
答案 0 :(得分:1)
使它成为IIFE(立即调用):
var groupSetter = (function groupSetter(){
console.log('asdfasdf');
return groupSetter;
})();
稍后重复使用:
$(window).resize(groupSetter);
答案 1 :(得分:1)
@ filur的答案不会起作用,因为它会立即调用该函数,返回undefined
,因此您无法恢复功能。
如果您希望将匿名函数表达式设置为groupSetter
,那么会出现什么问题:
(function() {
var groupSetter = function() {
console.log('asdfasdf');
};
$(window).resize(groupSetter);
})();