Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

JavaScript闭包问题求指教?

我需要一个函数默认执行一次,并且在jquery事件中还能被调用,目前卡在第二步了····

(function(){
    // 这样会自动运行一次
})()

$(window).resize(function () {
    // 我还想在这里调用上面那个函数
});

我试过这样:

var demo = (function(){
    // 这样会自动运行一次
})()

结果第二次调用就报错了,说不是一个函数、、、求大神指教下如何实现?前提是我不想直接是这样:

var demo = function(){
    // 这样会自动运行一次
};

demo()

$(window).resize(function () {
    demo();
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var demo = (function(){
    return arguments.callee
})()
或
var demo
;(demo = function(){})()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...