English 中文(简体)
j金花gin如何维持全球状态?
原标题:How do jQuery plugins maintain global state?

我最近撰写了我的第一个简单的 j子。 亲爱我。

http://jsfiddle.net/johnhoffman/wSeLY/1/

(function($) {
    $.fn.makeRed = function() {
        return this.each(function() {
            $(this).css("color", "#f00");
        });
    }
})(jQuery);

我很想知道,它为什么发挥作用。 我将 j子带入紧接的封闭功能中。

随后,在匿名职能范围内,没有地方变量吗? 它如何改变全球单一州j Query物体?

换言之,我是否只是通过<条码>在标的本地添加功能? 如何改变全球 j物,使我的职能(makeRed)能够在我的文字的全球范围内向挑选人提供?

最佳回答

isn t the |$| object a local variable within that anonymous function?

是的,$ 是该功能的一个当地变量,但这是一个大的变量,但指j。 彩票/代码 类似情况:

window.jQuery (global) ----->-----> { ... }
                                    ^
                                    |
$ (local) ------------------>-------+

因此,在你匿名后,你有两个变数点相同,这个目标仍然存在。

问题回答

Java文中的物体通过参考文件,因此,贵功能范围内的任何最新情况均保留在 j子上。 您只是以<条码>(<>$>/code>.

我高度建议你:。 Javascript: The Good Parts, it is all this.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.