Cannot understand chaining in Javascript!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • azegurb
    New Member
    • Sep 2009
    • 32

    Cannot understand chaining in Javascript!

    hi All,
    I have question around Douglas Crockford "method" method.
    Code:
    Function.prototype.method = function (name, func) {
        this.prototype[name] = func;
        return this;
    };
    function azerizor(value) {
    this.value=value;
    }
    
    
    azerizor.method('toString', function () {
    
        return this.value;
    });
    var me = new azerizor('salam').toString();
    
    alert(me);
    this is it. But here arises question regarding return this?
    what does mean here return this? this method works even if without return this this line. I have read about that. so that one say that it is for chaining. But i didnt understand how can i use chaining using this "method" function. Thanks in advance for attention. I will wait your responses!
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    May be chaining in this case won't be very helpful as it might turn your code unreadable. The idea is that you can call other methods on the return of the method function. For example, suppose you want to chain hashCode function to your aerizor object, you'd have something like this:
    Code:
    Function.prototype.method = function (name, func) {
        this.prototype[name] = func;
        return this;
    };
    function azerizor(value) {
    	this.value=value;
    }
    azerizor.method('toString', function () {
        return this.value;
    })[B].method('hashCode', function() {[/B]
    	  for(var ret = 0, i = 0, len = this.value.length; i < len; i++) {
    	    ret = (31 * ret + this.value.charCodeAt(i)) << 0;
    	  }
    	  return ret;
    });
    var v = new azerizor('Cheers');
    alert("String: " + v.toString() + ", Hash Code: "+ v.hashCode());

    Comment

    • Anas Mosaad
      New Member
      • Jan 2013
      • 185

      #3
      If you would like to chain functions of the same name you may update your method function implementation to something like the following:
      Code:
      Function.prototype.method = function (name, func) {
      	var fun = this.prototype[name];
      	if (typeof(fun) == "function") {
      		this.prototype[name] = function() {
      			fun.apply(this, arguments); 
      			return func.apply(this, arguments);
      		};
      	} else {
      	    this.prototype[name] = func;
      	}
      	return this;
      };
      I don't know how you will handle the return if the functions have a return. In my above example, I return the result of the last function on the chain.

      Comment

      • azegurb
        New Member
        • Sep 2009
        • 32

        #4
        Thank you very much for attention and spending time for this!

        Comment

        • azegurb
          New Member
          • Sep 2009
          • 32

          #5
          Thank you very much!

          Comment

          Working...