$ .Variable in jquery, some confusion in a certain way
I found some codes to check if the jquery plugin is correct. but I don't understand some of the differences between variable and function: code:
$.validator = function(options,form){
this.settings=$.extend(true,{},validator.defaults,options);
this.currentForm=form;
this.init();
}
but the validator is not a plugin, because the jQuery plugin must define $ .fn.validator. but it is also not a variable because $. in front of him.
Anyone who can explain this for me ???
source to share
They just add the function as a property to the function $
( jQuery
). Just like jQuery itself adds $.extend
and $.each
and to it $.ajax
, this validator plugin adds to it $.validator
. It is still a plugin (sort of), it just doesn't define a method that you can call on jQuery instances. You can only call one global version.
Given what the function does, it is expected to be called via new
, for example:
var v = new $.validator();
It really needs to have capital V
and it really needs to defend itself against a direct challenge, since what it is doing internally is really wrong if called directly.
source to share
A function is a piece of data.
A variable is something that you can store in a chunk of data.
A property on an object is something else that you can store in a chunk of data.
$
- variable. This value is a jQuery function.
$.validator
is a property of the jQuery function. The code you have is assigning a function to it.
$.fn.validator
will be a property of the object stored in a property fn
of the jQuery object. It can also have a value stored in it, but not in your code.
Function properties fn
can be bound to a jQuery object. those. with your existing code, you can call $.validator()
, but if you have $.fn.validator
, you can call $("div").validator()
.
source to share