How do I customize a class with jquery? (Demo reference)
/* Simple to use */
(function ($) {
El manipulated object, option property value
$.love = function (el,option) {
var lo = $ (EL);
var lo.vars = $.extend ({},$.love.default, option); Merged into a new object, the new property list
Defining additional properties
......
var method = {};
Private methods, private methods can be called between each other
method={
Functiona:function () {...},
Functionb:function () {...},
Functionc:function () {...},
...
}
Public method (privileged method) for out-of-class invocation
This.publicfunction = function (a,b,c) {
....
/* Call private Function */
Method.functiona ();
...
}
...
}
Default properties can be set
$.love.default = {
Option1: ...,
Option2: ...,
....
}
}) (JQuery);
/* Out-of-class use */
var a = new $.love ("#id", {title: "Name", Age:12,...});
A.publicfunction (A,B,C);
/* Relative High Point */
(function ($) {
El manipulated object, option property value
$.love = function (el,option) {
var lo = $ (EL);
var lo.vars = $.extend ({},$.love.default, option); Merged into a new object, the new property list
Defining additional properties
......
var method = {};
$.data (El, "Love", lo); Storing data on elements, including all properties of Lo, method
Private methods, private methods can be called between each other
method={
Functiona:function () {...},
Functionb:function () {...},
Functionc:function () {...},
...
}
Public method (privileged method) for out-of-class invocation
Lo.pfunctiona = function () {
/* Call private Function */
Method.functiona ();
},
LO.PFUNCTIONB = function () {...},
...
}
Default properties can be set
$.love.default = {
Option1: ...,
Option2: ...,
....
}
$.fn.love (option) {
var $this = $ (this);
if ( $this. Data (' Love ') ===undefined) {
New $.love (this,option);
}else{
var love = $this. Data (' love '); Direct use of functions in classes, etc.
Love.pfunctiona ();
}
}
}) (JQuery);
$ ==> "$" is a reference to a jquery object, equal to "jquery"
(function () {}) ==> mimic block-level scope
$.xxx ==> Adding methods for JQuery objects (I understand)
$.fn.xxx = = "Add method to Element (I understand)
Encapsulation of jquery Custom classes