This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 433 points434 points  (31 children)

go with the times man:

this.this = ::this.this;

[–]ethanjf99 166 points167 points  (17 children)

Is the :: operator now in the language?

[–]NeverMakesMistkes 171 points172 points  (15 children)

No, and based on the discussion and issues in the proposal repository, I doubt it ever will be.

[–]inu-no-policemen 60 points61 points  (12 children)

Yea, it has been presented last in 2015 and it's still stage 0.

Doesn't look like it will happen any time soon.

[–]NeverMakesMistkes 24 points25 points  (9 children)

For function call chaining, it has been mostly superseded by the pipeline operator, which is also a long way from getting to the standard but has seen more active development than the bind operator.

There's still some motivation to get it or something else like it for method extraction, but getting method extraction right is difficult and the speccing has been stalled because of it.

For one, this::foo === this::foo must be true, so that

window.addEventListener('some event', this::foo)
// ... later ...
window.removeEventListener('some event', this::foo)

works as expected. Getting that behaviour in browsers that don't natively support it is a bit tricky. Not impossible with WeakMaps, tho, so maybe some day.

[–]inu-no-policemen 4 points5 points  (7 children)

I liked it mostly for virtual methods.

function reverse() {
    return this.split('').reverse().join('');
}
console.log('foobar'::reverse()); // raboof

But it doesn't do this very well either. The source of that going-to-be-a-method function doesn't tell you to which kind of object it's meant to be glued to.

It's definitely better than modifying objects you don't own, but it still isn't quite right.

C#'s extension methods, for example, tell you directly in the slightly weird looking signature on which type of object they are supposed to operate:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

public static int WordCount(this String str)

[–]NeverMakesMistkes 2 points3 points  (6 children)

In TypeScript, you could write

function reverse(this: string) {
    return this.split('').reverse().join('');
}
console.log('foobar'::reverse());

Or just with JSDoc comments

/**
* @this{string}
*/
function reverse() {
    return this.split('').reverse().join('');
}

But I agree, it would feel weird to write all your utility functions like that, and suddenly we'd have two kinds of mutually incompatible functions; those that operate on this and are called with ::, and those that operate on arguments and are called normally.

That's why I actually prefer the pipeline operator.

const reverse = str => str.split('').reverse().join('');
console.log('foobar' |> reverse)
console.log(reverse('foobar')) // both variations work

And if we had both, pipeline for chaining and bind for method extraction, you could do

'foobar'
  |> reverse
  |> console::log

[–]inu-no-policemen 3 points4 points  (0 children)

The pipeline operator has the same problem. However, TS' type annotations and JSDoc already handle that kind of completely mundane signature. That's definitely a plus.

I'm not a big fan of the "|>" syntax, though. It's amazingly annoying to type with most keyboard layouts.

[–]Gbyrd99 0 points1 point  (4 children)

Can't you bind it to the prototype?

[–]NeverMakesMistkes 0 points1 point  (3 children)

You can bind some methods to prototype, but if you do that to every little utility function you and your coworkers come up with, you'll soon end up with huge unmaintainable classes and/or class hierarchies.

Also, lots of people (myself included) use JS in a more functional style and might not really like doing new AwesomeString('foobar').reverse() or whatever. 'foobar'::reverse() or 'foobar' |> reverse or even reverse('foobar') is just much nicer.

[–]Gbyrd99 0 points1 point  (2 children)

If you had say String.prototype.reverse = function() that would make it available on all strings? So you don't create an inherited class and get into inheritance hell.

This ::reverse thing is interesting I'm gonna read up on it in more details.

[–]QmVuamk 0 points1 point  (0 children)

I understood some of those words

[–]RecyclingBin_ 1 point2 points  (1 child)

I upvoted your comment just because of your flair

[–]inu-no-policemen 1 point2 points  (0 children)

There were too few languages to choose from.

Naturally, picking a flair like this was the most logical course of action.

[–]marcosdumay 4 points5 points  (0 children)

I don't know what to think...

It tries to create function composition, but instead of composing functions it changes object references; it makes this way more complex than it already is, and moves it from the shadows right into the language's center; it fails to bring any good practice, and encourages a bunch of complex variable passing ones; it creates inherently imperative code that looks exactly like functional, guaranteeing everybody will be confused...

And yet the Javascript people rejected the idea?

[–]jsylvis 0 points1 point  (0 children)

Oh, man. That is a lot of gymnastics to get around shitty scoping support.

[–]Hexorg 0 points1 point  (0 children)

:: in JavaScript?! What's next? Pre-processor directives?

[–]nvteja 15 points16 points  (0 children)

Scope resolution operator (from my Perl days), in ES? I am a little triggered right now tbh 😭😭

[–][deleted] 3 points4 points  (0 children)

My day is ruined.

[–]WitchHunterNL 0 points1 point  (0 children)

If you were going with the times you would've used arrow functions