473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Cannot understand chaining in Javascript!

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
4 1449
Anas Mosaad
185 128KB
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
Anas Mosaad
185 128KB
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
azegurb
32
Thank you very much for attention and spending time for this!
Jan 28 '13 #4
azegurb
32
Thank you very much!
Jan 28 '13 #5

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

Similar topics

1
by: Jing You | last post by:
hi every one, I have got some confused problem when I try to write some custom object by javascript. Look at the example code here: <BODY> <script language="jscript">
7
by: tuchka | last post by:
Hi, guys! I am very new here and just started to learn C. I have previous java exp. however. I'm abs. stuck on pointers and i'm unable comprehend algorithm of simple program that reverses chars...
5
by: rashmi | last post by:
Hello All, tp.c:107: error: syntax error before '*' token tp.c:108: warning: function declaration isn't a prototype tp.c:121: error: syntax error before '*' token tp.c:122: warning: function...
3
by: Lionheart | last post by:
using System namespace ConsoleApplication1 public class Class public static void Main( for (int i = 1; i < 10; i++
17
by: Gladiator | last post by:
When I am trying to execute a program from "The C Programming Language" by Dennis Ritchie, I tried to run the following program.I am using Dev++ as a compiler software. The Program is presented...
4
by: dennise9 | last post by:
My exeternal JS works fine when the call is placed in the page head or body. But if I call the same javascript from a (clicked) text link on the page, the script throws a JS error when it...
2
by: Ming | last post by:
I do not understand the sample code on php.net for curl_multi_exec. Any help? Many thanks! <?php // create both cURL resources $ch1 = curl_init(); $ch2 = curl_init(); // set URL and other...
9
by: reachmsn | last post by:
Hi, At the url http://www.python.org/doc/essays/graphs.html there is some code by Guido Van Rossum for computing paths through a graph - I have pasted it below for reference - Let's write a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.