Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think you're confusing what's happening here?

The first example is doing a normal string interpolation, and then passing the result to a function. The latter is doing template literals, which is a whole other feature. You're not really "running a function without parenthesis".



So you're not running a function when I write "f`some ${foo} lols`" ? What's f then ? This is HN and I'm a self taught noob, so probabilistically I'm wrong and you're right. But I'm having such a hard time believing it.

To me, it's still running the sequence of instructions defined by f in both cases. Just that the arguments passed to f are different whether it's called with parenthesis or not. And as far as I understand, that's the feature that comes with string literals. But you're still calling f, and f is still a function. It's defined as a function and executed as a function.

"If there is an expression preceding the template literal (tag here), this is called a "tagged template". In that case, the tag expression (usually a function) gets called with the processed template literal, which you can then manipulate before outputting." - [1]

Now when i open my console (FF) and I type:

  a = 12
  a`hello`
I get "TypeError: a is not a function". But if I do

  b = () => "yo"
  b`hello`
I get, as expected, "yo" and no TypeError.

Furthermore, I didn't know this feature, the article called it "functions without using parenthesis" so i called it like that.

N.B. I'd like to know what the interpreter / JIT is doing now ;p

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...


Eventually, f is called as a function, but in the syntax tree, it's not a direct "function call". The obvious way to see that is, as you can see in your example, "hello" isn't passed as an argument. Instead, there's an intermediate form internally that does some magic, and calls the function with some other parameters.

Maybe I was being a little pedantic, but yeah, f`test` and f(`test`) are two very different operations under the hood.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: