jQuery plugins provide developers an easy way to create reusable code. There are hundreds of existing plugins, but you can improve your JavaScript skills by writing your own.
$.fn.PluginName = function () {
elms = this;
}
$('span').PluginName();
'$' is the jQuery base object. fn is like prototype in JavaScript.
Plugins become more helpful when you are able to leverage their customization. We can customize options by using the jQuery extend function.
$.fn.PluginName = function (options) {
elms = this;
//Outputs black
console.log(options.color);
}
$('span').PluginName({color: '#000'});
We can leverage jQuery's $.extend method to merge your options.
See the Pen jQuery Extend by Jon (@Middaugh) on CodePen.
Passing an empty object as the target preserves the original objects.
See the Pen Basic jQuery Plugin by Jon (@Middaugh) on CodePen.
See the Pen Basic jQuery Plugin Basic Events Each by Jon (@Middaugh) on CodePen.
Stores data for one session (data is lost when browser is closed).
Stores data with no expiration date.
if(typeof(Storage) !== "undefined") {
localStorage.setItem("lastname", "Smith");
}else{
//Some graceful fallback plan
}
You can use this to mock a back end on demo day, but....
Never store sensitive data in web storage. If a malicious script were to run in your browser, it can access web storage and send the data anywhere.
There are a number of solutions for mocking data in your app. This can also be known as backendless development.