jQuery Plugin

A jQuery plugin based on functional programming - there is no Object contructor call, no method declarations using prototye, no use of "this" to call methods with all the confusion and clumsiness involved with that.

by Patrick Hund

HTML

<input id="input1">
<input id="input2">
<input id="input3">
<input id="input4">
<input id="input5">
<div id="logBox"></div>
<button>change placeholder</button>

CSS

#logBox {
    background-color: black;
    color: lime;
    font-family: monospace;
}

JavaScript

var pluginName = "placeholder";

$.fn[pluginName] = function (input) {

    var invocations = [];

    this.each(function () {
        var $element = $(this), // jquery element that the plugin instance is working on
            defaults, // default settings
            settings, // settings of this plugin instance
            init, // initialization functions
            handle, // event handler functions
            element; // functions for checking and manipulating the element

        defaults = {
            text: "foo",
            log: function (message) {
                console.log(message);
            }
        };

        init = {
            settings: function () {
                switch (typeof input) {
                    case "object":
                        settings = $.extend(true, defaults, input);
                        break;
                    case "undefined":
                        settings = defaults;
                        break;
                    case "string":
                        settings = defaults;
                        settings.text = input;
                        break;
                    default:
                        throw pluginName + " plugin initialization failed - " +
                            "invalid input type: " + (typeof input);
                }
                return init;
            },
            handlers: function () {
                $element.on("focus", handle.focus);
                $element.on("blur", handle.blur);
                return init;
            },
            plugin: function () {
                var plugin = $element.data("plugin_" + pluginName);
                if (plugin) {
                    if (typeof input !== "string") {
                        throw "error calling method on plugin " + pluginName + ": argument needs to be type string (method name)";
                    }
                    if (typeof plugin[input] !== "function") {
                        throw "error...