By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,711 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,711 IT Pros & Developers. It's quick & easy.

Cannot understand chaining in Javascript!

P: 32
hi All,
I have question around Douglas Crockford "method" method.
Expand|Select|Wrap|Line Numbers
  1. Function.prototype.method = function (name, func) {
  2.     this.prototype[name] = func;
  3.     return this;
  4. };
  5. function azerizor(value) {
  6. this.value=value;
  7. }
  8.  
  9.  
  10. azerizor.method('toString', function () {
  11.  
  12.     return this.value;
  13. });
  14. var me = new azerizor('salam').toString();
  15.  
  16. 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!
Jan 27 '13 #1
Share this Question
Share on Google+
4 Replies


100+
P: 185
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:
Expand|Select|Wrap|Line Numbers
  1. Function.prototype.method = function (name, func) {
  2.     this.prototype[name] = func;
  3.     return this;
  4. };
  5. function azerizor(value) {
  6.     this.value=value;
  7. }
  8. azerizor.method('toString', function () {
  9.     return this.value;
  10. }).method('hashCode', function() {
  11.       for(var ret = 0, i = 0, len = this.value.length; i < len; i++) {
  12.         ret = (31 * ret + this.value.charCodeAt(i)) << 0;
  13.       }
  14.       return ret;
  15. });
  16. var v = new azerizor('Cheers');
  17. alert("String: " + v.toString() + ", Hash Code: "+ v.hashCode());
  18.  
  19.  
Jan 27 '13 #2

100+
P: 185
If you would like to chain functions of the same name you may update your method function implementation to something like the following:
Expand|Select|Wrap|Line Numbers
  1. Function.prototype.method = function (name, func) {
  2.     var fun = this.prototype[name];
  3.     if (typeof(fun) == "function") {
  4.         this.prototype[name] = function() {
  5.             fun.apply(this, arguments); 
  6.             return func.apply(this, arguments);
  7.         };
  8.     } else {
  9.         this.prototype[name] = func;
  10.     }
  11.     return this;
  12. };
  13.  
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.
Jan 27 '13 #3

P: 32
Thank you very much for attention and spending time for this!
Jan 28 '13 #4

P: 32
Thank you very much!
Jan 28 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.