最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript module pattern: How to injectcreateextend methodsplugin to our own library? - Stack Overflow

programmeradmin1浏览0评论

Im new to javascript. Sorry if there is anything wrong with my question.

How to inject/create/extend methods or plugins to our own library? here is "yourlib.js"

var Yourlib = (function() {
    // privt. var
    var selectedEl = {}

    // some privt. funct
    function something() {

    }

    return {
        getById : function() {

        },
        setColor : function() {

        }
    }
}());

And below is your "plugin.js"

/*
How to create the plugin pattern?
Example: I want to create/inject a method/plugin named "setHeight" .
So, i can call it later, like this: Yourlib.getById('an-id').setHeight(value);
How is the pattern?
*/

Im new to javascript. Sorry if there is anything wrong with my question.

How to inject/create/extend methods or plugins to our own library? here is "yourlib.js"

var Yourlib = (function() {
    // privt. var
    var selectedEl = {}

    // some privt. funct
    function something() {

    }

    return {
        getById : function() {

        },
        setColor : function() {

        }
    }
}());

And below is your "plugin.js"

/*
How to create the plugin pattern?
Example: I want to create/inject a method/plugin named "setHeight" .
So, i can call it later, like this: Yourlib.getById('an-id').setHeight(value);
How is the pattern?
*/
Share Improve this question asked May 28, 2013 at 8:00 Jahe JSJahe JS 911 silver badge9 bronze badges 1
  • I think this question belongs on programmers.stackexchange.com – sroes Commented May 28, 2013 at 8:04
Add a comment  | 

3 Answers 3

Reset to default 9

You need to return this within your functions to make them chainable. The example code below shows how to extend your module to allow chained calls and add new functions if needed.

var Yourlib = (function() {

  // privt. var
  var selectedEl = {}

  // some privt. funct
  function something() {


  }

  return {

    setColor: function(newcolor) {

      var obj = document.getElementById('colorbox1');
      obj.style.background = newcolor;

    }
  }

}());

// call the original module sub-function
Yourlib.setColor('blue');

/**
 * Extend the module again to allow chaining.
 * Chainable functions return "this"
 */
var Yourlib = (function(object) {

  // private variable to store id of a box
  var box = '';

  object.getById = function(id) {

    box = document.getElementById(id);
    return this;
  };

  object.setColor = function(newcolor) {

    box.style.background = newcolor;
    return this;

  };

  object.setAnotherColor = function(newcolor) {

    var box = document.getElementById('colorbox4');
    box.style.background = newcolor;

  };

  return object; // return the extended object

}(Yourlib)); // original module passed in as an object to be extended


// example of a chained function call 
Yourlib.getById('colorbox2').setColor('purple');

// same functions without chained call
Yourlib.getById('colorbox3')
Yourlib.setColor('green');

// new function within extended module
Yourlib.setAnotherColor('orange');
.colorbox {
  height: 40px;
  width: 40px;
  background: #000;
  border: #000 1px solid;
  margin-bottom: 5px;
}
<strong>module sub-function</strong>
<br />Yourlib.setColor('blue');
<br />
<div id="colorbox1" class="colorbox"></div>

<strong>chained function calls</strong>
<br />Yourlib.getById('colorbox2').setColor('purple');
<br />
<div id="colorbox2" class="colorbox"></div>

<strong>functions called without chaining</strong> 
<br />Yourlib.getById('colorbox3')
<br />Yourlib.setColor('green');
<br />
<div id="colorbox3" class="colorbox"></div>

<strong>new function added to extended module</strong>
<br />Yourlib.setAnotherColor('orange');
<br />
<div id="colorbox4" class="colorbox"></div>

For further reference you may also read JavaScript Module Pattern in Depth.

Notice that your function returns an object with two methods. You can directly add properties to it:

Yourlib.setHeight = function (val) {
    // logic for setting the height
};
var Module = (function(){
 var set = {}
 set.show = function(){
   alert('Module method')
 }
 return set
})()

So now I will extend current Module.

var Ext = (function(Module){

 Module.get = function(){
   Module.show()
 }

 return Module

})(Module)

Now I can do this:

Module.get()
Ext.get()
发布评论

评论列表(0)

  1. 暂无评论