As covered earlier, in JavaScript, we donât have the traditional notion of classes that we would find in other classical programming languages, but we do have prototypal inheritance. With prototypal inheritance, an object inherits from another object. We can apply this concept to jQuery plug-in development.
Yepnope.js author Alex Sexton and jQuery team member Scott Gonzalez have looked at this topic in detail. In sum, they discovered that for organized modular development, clearly separating the object that defines the logic for a plug-in from the plug-in generation process itself can be beneficial.
The benefit is that testing our plug-inâs code becomes significantly easier, and we are also able to adjust the way things work behind the scenes without altering the way that any object APIs we implement are used.
In Sextonâs article on this topic, he implemented a bridge that enables us to attach our general logic to a particular plug-in, which weâve implemented in the pattern below.
One of the other advantages of this pattern is that we donât have to constantly repeat the same plug-in initialization code, thus ensuring that the concepts behind DRY development are maintained. Some developers might also find this pattern easier to read than others.
/*!
* jQuery prototypal inheritance plugin boilerplate
* Author: Alex Sexton, Scott Gonzalez
* Further changes: @addyosmani
* Licensed under the MIT license
*/
// myObject - an object representing a concept we wish to model
// (e.g. a car)
var
myObject
=
{
init
:
function
(
options
,
elem
)
{
// Mix in the passed-in options with the default options
this
.
options
=
$
.
extend
(
{},
this
.
options
,
options
);
// Save the element reference, both as a jQuery
// reference and a normal reference
this
.
elem
=
elem
;
this
.
$elem
=
$
(
elem
);
// Build the DOM's initial structure
this
.
_build
();
// return this so that we can chain and use the bridge with less code.
return
this
;
},
options
:
{
name
:
"No name"
},
_build
:
function
(){
//this.$elem.html( "<h1>"+this.options.name+"</h1>" );
},
myMethod
:
function
(
msg
){
// We have direct access to the associated and cached
// jQuery element
// this.$elem.append( "<p>"+msg+"</p>" );
}
};
// Object.create support test, and fallback for browsers without it
if
(
typeof
Object
.
create
!==
"function"
)
{
Object
.
create
=
function
(
o
)
{
function
F
()
{}
F
.
prototype
=
o
;
return
new
F
();
};
}
// Create a plug-in based on a defined object
$
.
plugin
=
function
(
name
,
object
)
{
$
.
fn
[
name
]
=
function
(
options
)
{
return
this
.
each
(
function
()
{
if
(
!
$
.
data
(
this
,
name
)
)
{
$
.
data
(
this
,
name
,
Object
.
create
(
object
).
init
(
options
,
this
)
);
}
});
};
};
Usage:
$
.
plugin
(
"myobj"
,
myObject
);
$
(
"#elem"
).
myobj
(
{
name
:
"John"
}
);
var
collection
=
$
(
"#elem"
).
data
(
"myobj"
);
collection
.
myMethod
(
"I am a method"
);
Using Inheritance Patterns To Organize Large jQuery Applications, Alex Sexton
How to Manage Large Applications With jQuery or Whatever (further discussion), Alex Sexton
Practical Example of the Need for Prototypal Inheritance, Neeraj Singh
Prototypal Inheritance in JavaScript, Douglas Crockford
Get Learning JavaScript Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.