javascript和fn发生了什么
What is going on with the javascript and the fn?
可能重复:
jQuery.fn是什么意思?
我在一个页面上有一些脚本,试图弄清楚它的作用:
(function ($, window, undefined)
{
var
// String constants for data names
dataFlag = "watermark",
dataClass = "watermarkClass",
dataFocus = "watermarkFocus",
dataFormSubmit = "watermarkSubmit",
dataMaxLen = "watermarkMaxLength",
dataPassword = "watermarkPassword",
dataText = "watermarkText",
// Copy of native jQuery regex use to strip return characters from element value
rreturn = /'r/g,
// Includes only elements with watermark defined
selWatermarkDefined = "input:data(" + dataFlag + "),textarea:data(" + dataFlag + ")",
// Includes only elements capable of having watermark
selWatermarkAble = "input:text,input:password,input[type=search],input:not([type]),textarea",
// triggerFns:
// Array of function names to look for in the global namespace.
// Any such functions found will be hijacked to trigger a call to
// hideAll() any time they are called. The default value is the
// ASP.NET function that validates the controls on the page
// prior to a postback.
//
// Am I missing other important trigger function(s) to look for?
// Please leave me feedback:
// http://code.google.com/p/jquery-watermark/issues/list
triggerFns = [
"Page_ClientValidate"
],
// Holds a value of true if a watermark was displayed since the last
// hideAll() was executed. Avoids repeatedly calling hideAll().
pageDirty = false,
// Detects if the browser can handle native placeholders
hasNativePlaceholder = ("placeholder" in document.createElement("input"));
/*******************************************************/
/* Enable / disable other control on trigger condition */
/*******************************************************/
$.fn.TriggerContol = function (options)
{
最后两行字我很吃力。为什么开发人员使用fn,这意味着什么?
我知道整个文件基本上是一个自调用的匿名函数,旨在将函数附加到Jquery库中,这样你就可以执行Jquery(x(.TriggerControl。我只是想知道这个特定的构造意味着什么。
在jQuery中,jQuery.fn
只是对jQuery.prototype
的引用。
因此,要了解正在发生的事情,您确实需要了解原型继承。
从jQuery
构造函数创建的所有对象都将继承jQuery.prototype
对象上存在的任何属性。
举个简单的例子:
function jQuery() {
// empty constructor function
}
jQuery.fn = jQuery.prototype; // Make a "fn" property that simply references the
// jQuery.prototype object.
// So assigning a property to jQuery.fn
// is the same as assigning it to jQuery.prototype
jQuery.fn.sayHello = function() { alert( "Hi!" ); };
// Create an object from the jQuery constructor
var obj = new jQuery;
// Our new object will automatically inherit the "sayHello" function
obj.sayHello(); // "Hi!"
这显然非常简单,jQuery所做的远不止这些,但正是原型继承允许所有jQuery对象具有相同的方法集。
要向所有jQuery对象添加一个新方法,只需向其原型添加即可。
jQuery.fn.sayGoodbye = function() { alert("Goodbye!"); };
// call the new method on our original object
obj.sayGoodbye(); // Goodbye!
正如您所看到的,sayGoodbye
神奇地出现了,尽管我们没有直接将该属性添加到obj
中。
JSFIDDLE演示以上代码。
我认为他们提供jQuery.fn
作为属性的原因很简单,因为它更短,对于不知道原型继承是如何工作的人来说,也许更容易理解。
使用fn运算符,您可以为(一组(元素创建自己的方法。在这种情况下,该方法被称为TriggerControl,可以被称为:
$('.someclass').TriggerControl(someoptions);
查看此页面了解更多信息。
相关文章:
- Javascript函数 - 通过引用复制,但这里发生了什么
- 套接字发生了什么's在'断开连接'事件(服务器端)
- 这段代码中发生了什么
- 通过它访问HTML元素's id DIRECTLY-这里发生了什么
- JavaScript's数组过滤器函数在没有分配函数的情况下使用-这里发生了什么
- 这个JavaScript函数中发生了什么
- 我的输入字段听到除回车之外的每个“键下”事件.我有一个日期选择器,不确定发生了什么
- isPrototypeOf 说不,但实例说是——发生了什么
- 十分钟内的 Javascript:此示例代码中发生了什么说明惰性范围
- 数字值正在更改,任何人都知道发生了什么
- 当需要(“http”)时发生了什么.Server() 以 Express 应用程序作为其参数进行评估
- 有人请帮助解释这个javascript代码,即发生了什么以及代码在做什么
- jQuery 1.4.4 和 1.7.2 之间的 .ajax() 发生了什么导致代码中断
- 此代码中发生了什么,如果没有“with”关键字,以下内容的基本实现是什么
- 这个JavaScript三元运算符中发生了什么
- 有人能告诉我循环中发生了什么吗
- 按下escape退出全屏时发生了什么?我如何用按钮复制它
- 触摸启动与点击.引擎盖下发生了什么
- Handlebars.js中的__get__函数发生了什么
- 有人能帮我理解这个代码中发生了什么吗