English 中文(简体)
这些职能有什么区别?
原标题:What is the difference between these functions?

我有一本《Jquery in Action》的书,在谈到消除与其他图书馆的冲突时,提到了这三项职能。 然而,我不知道两者之间有何区别,不理解这本书的解释。

jQuery(function($) {
    alert( I"m ready! );
});

var $ =  Hi! ;
jQuery(function() {
    alert( $ =   + $);
});

var $ =  Hi! ;
jQuery(function($) {
    alert( $ =   + $);
});

是否有任何人知道差别? 感谢。

最佳回答

如果你采用简化版本,则可能更容易理解。 第一个现成的职能并不超出警惕。 其余两个是有趣的。

职能范围很广,这意味着,如果你在内部使用一个变量,那么,在确定这一变量之前,它就将进入等级。

在您的第二次准备职能中,<>$> > 至<>>。 如果你开始履行职务,则没有其他<>$>。

However, in the third ready block, the $ will not go to the Hi! because it has a definition that is closer - the one passed as an argument (function($) {). This $ will be the jQuery function (i.e. in that function $ == jQuery) as this is how jQuery s ready feature is implemented.

因此:

var $ =  Hi! ;

jQuery(function() {
    alert( $ =   + $); // in this scope, $ will refer to the  Hi! 
});

jQuery(function($) {   // the $ here will  shadow  the $ defined as  Hi! 
    alert( $ =   + $); // in this scope, $ will refer to jQuery
});

现在,你的问题是与其他图书馆的冲突。 其他图书馆(例如原型)也使用$的文号,因为它是召集图书馆的方便捷径。 如果你利用你提供的最后一次现成职能,你可以sure,在该职能范围内,$将提及“j Query”作为“j Query”转而担任这一职务(作为第一个论点)。

在第二个现成职能中,$可能也已归入原型,例如,你无法确定你是用$/code>。 举例来说,它为<代码>。 Hi!,而不是j Query。 如果是这样的话,它就属于同样的事情。 考虑:

// Prototype is loaded here, $ is referring to Prototype

jQuery(function() {
    $( selector ).addClass( something ); // Oops - you re calling Prototype with $!
});

另一方面:

// Prototype is loaded here, $ is referring to Prototype

jQuery(function($) { // this $ is shadowing Prototype s $, this $ is jQuery
    $( selector ).addClass( something ); // Yay - you re calling jQuery with $
});
问题回答

页: 1 彩票(功能费){,美元只是 j子。 事实上,你可以使用任何法律识别符号,而不是美元,这仍然是 j子的 alia子。

在第二个例子中,报警费将达4美元。 既然如此,那一美元就是指仅仅高于这一功能的老鼠。

第三个例子实际上掩盖了该职能上所申报的美元,因为这笔钱将在本职能范围内向 j子支付。

I hope this makes sense to you.

第一组法典宣布,一旦人力部配备齐全,即可执行。 它只是制作一个警示箱。

jQuery(function($) {
    alert( I"m ready! );
});

第二组法典也宣布了一名现成的手稿,但它正在显示冲突。 <><>><> 代码/代码>变量被故意设定为座标(可能来自相互冲突的舱位图书馆),因此不能用作舱面标。 在手稿功能范围内,$> 仍被分配到座标上,因此,警报显示$ = Hi!

var $ =  Hi! ;
jQuery(function() {
    alert( $ =   + $);
});

The third code block also declares a ready handler, but it declares a local $ parameter. The jQuery object passes itself into the ready handler as the first parameter and so within the function scope, $ refers to the jQuery object and the alert will print $ = ..., where ... is a representation of the jQuery object.

var $ =  Hi! ;
jQuery(function($) {
    alert( $ =   + $);
});




相关问题
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.