1

So I wrote this String function to add stuff to each word in a sentence

String.prototype.addStuff = function() {
    return this.replace(/ /g, "-stuff ") + "-stuff"
}

I was told there was a way to invoke this function on a string without using parentheses. Anyone know how?

I tried wrapping the function declaration in parentheses and adding a pair to the end:

String.prototype.liu = (function() {
    return this.replace(/ /g, "-liu ") + "-liu"
})();

But that didn't work :/

Ldrbrandon
  • 59
  • 8
  • You would never wrap it in parenthesis. You would call your function externally. – Nicholas Hazel Feb 05 '14 at 04:14
  • [Read this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain) – Nicholas Hazel Feb 05 '14 at 04:15
  • If you “were told”, then why don’t you ask the person you _were told_ by …? (Only thing I can think of, is a `toString` method that will get called automatically when the string is output somewhere.) – CBroe Feb 05 '14 at 04:16
  • Including a formal parameter list in parenthesis after an expression that resolves to a function (e.g. a variable name or function expression) causes the function to be called. There is no other way without [getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get) and [setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/set?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FOperators%2Fset), which aren't generally available. See [ECMA-262 §11.2.3 Function Calls](http://ecma-international.org/ecma-262/5.1/#sec-11.2.3). – RobG Feb 05 '14 at 04:22
  • See [Invoking a function without parentheses](https://stackoverflow.com/questions/35949554/invoking-a-function-without-parentheses) for lots of ways to do this. – trincot Oct 11 '18 at 15:07

2 Answers2

6

You can, with Object.defineProperty and providing an accessor descriptor:

Object.defineProperty(String.prototype, 'liu', {
    get: function() {
        return this.replace(/ /g, "-liu ") + "-liu"
    }
});

"foo".liu

Although I don't know why you'd want to do that.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Only in ES5 hosts though, e.g. IE 8 is out. :-( – RobG Feb 05 '14 at 04:50
  • Thanks, that works and it's cool. I was asked it in an interview and was completely stumped. I wrote the String function to add -liu to the end of each word and then my interviewer asked me, "what if I want to be able to call it like so: "hello my name is".liu and it would return "hello-liu my-liu name-liu is-liu" – Ldrbrandon Feb 05 '14 at 17:25
0

Potentially, you can't just wrap a whole function into a prototype to auto call.

Now, what you can do, is define a shared function within that prototype.

Of course, you have to call it, but that beats no answer at all.

Good Read on how prototypical inheritance works

Now, you COULD do:

yourFunc.prototype.call('yourFunc');

But you'd have to format it properly to make it work.

Nicholas Hazel
  • 3,758
  • 1
  • 22
  • 34