0

How must a function be 'chained', in order to call this function like this

F('Test').custom_substring(0,1);
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
enzian
  • 1,607
  • 3
  • 18
  • 19

3 Answers3

2

You have to return an object that has a method member named custom_substring. One example:

var F = function(){
  return {
    custom_substring:function(){
      console.log('custom substring');
      return this;
    }
  }
}

F('Test')
  .custom_substring(0,1)
  .custom_substring(0,1)
  .custom_substring(0,1);

To create objects you can use constructor functions and prototype, this is a complex subject and explained here.

I would not mess with the String.prototype because that breaks encapsulation.

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160
  • How can I return 'Test' back if I want only call F('Test'); ? – enzian Sep 02 '14 at 19:11
  • @user3744885 either add custom_substring to String.prototype but I would not advice it because that breaks encapsulation or implement a valueOf and toString method in the returned object. – HMR Sep 03 '14 at 06:42
1

The following sample provides a chainable custom_substring that does not modify the original object but instead returns a new one. This is similar to how jQuery and other libraries work (and how built-in string operations work) and it helps make for safer and more predictable code.

function F(str) {
    return {
        toString: function () { return str; },

        // You didn't provide an example of what you want custom_substring
        // to do, so I'll have it append a "!" to the beginning of the resulting value
        // (since I can't think of anything else for it to do)
        custom_substring: function (from, to) {
            return F("!" + str.substring(from, to));
        }
    };
}

var s1 = F("Hello everyone");
var s2 = s1.custom_substring(0, 7);
var s3 = s2.custom_substring(0, 5)
           .custom_substring(0, 4);

console.log(s1);  // Hello everyone
console.log(s2);  // !Hello e
console.log(s3);  // !!!He
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Is there a equivalent for toString: ..., if you have (like in libraries) a string as input and want to return an element as 'default'? – enzian Sep 02 '14 at 19:32
  • @user3744885 Could you give a more specific example? I'm not sure what you mean. – JLRishe Sep 02 '14 at 20:22
  • I want a function I call F('id_of_an_element'), that returns me the document.getElementById() of the parameter, but I still want to have other 'subfunctions' like F('test').custom_substring; – enzian Sep 02 '14 at 20:39
  • @user3744885 Ok, and what would `custom_substring` do in this case (if `F('test')` is selecting elements. And have you considered creating a jQuery plugin for this instead of reinventing the wheel? – JLRishe Sep 03 '14 at 03:36
-1

If you really want to create chainloading you need always return this from methods where it's possible.

For example we have some class with some methods:

function Foo() {
    this.foo = 'bar';
    return this;
}

Foo.prototype = Object.create({
    sayFoo: function() {
        console.log(this.foo);
        return this;
    },

    getFoo: function() {
        return this.foo; // Here you can't make chainload
    },

    saySmth: function() {
        console.log('Something');
        return this;
    }
});

And we can use this:

var test = new Foo().sayFoo().saySmth().getFoo(); // prints this.foo -> 'Something' -> returns this.foo
console.log(test); // prints out this.foo
Eugene Obrezkov
  • 2,910
  • 2
  • 17
  • 34
  • 1
    WTH is `Object.create` good for? Btw, you don't need to `return this` from a constructor – Bergi Aug 31 '14 at 12:48
  • @Bergi Are you seriously? Object.create creates object with prototype, read ECMA-262. For creating chainloading effect needs return this from a constructor. Oh man, you need read a lot of books. – Eugene Obrezkov Aug 31 '14 at 14:45
  • 2
    I know very well what it does, I'm trying to tell you that it's useless (if not wrong) where you use it. And no, you don't need to `return this` from constructors. Oh man, you should throw away your books (I have tried reading a few, but I mostly found them sprinkled with mistakes). – Bergi Aug 31 '14 at 14:52