7

What is the equivalent of Java double colon operator in typescript(if there's any):

stream.forEach( System.out::println(s));

EDIT: I know it's doable with certain functions like:

stream.forEach(console.log);
stream.filter(Boolean);

But when I use other functions e.g. BehaviorSubject "next" my code breaks. I don't know what qualifies those other two to be called by reference. I'd like to have something like:

stream.pipe(someSubject.next);

Instead of:

stream.pipe(value => someSubject.next(value));
Wildhammer
  • 2,017
  • 1
  • 27
  • 33
  • 4
    Are you sure that it's `stream.forEach( System.out::println(s));` and not `stream.forEach( System.out::println);` ? – izogfif Apr 13 '22 at 12:58

1 Answers1

6

Okay, so I did a bit of searching and there is a double colon operator in JScript, which was Microsoft's implementation of JavaScript:

a double colon is used as separator between the script ID and the event name

My guess is that's not part (or no longer part) of Internet explorer's ECMAScript implementation but it belongs (or used to belong) to Microsoft Office's implementation

What does ‘::’ (double colon) do in javascript for events?

Which was a really cool find, but I've never seen a double colon operator being used in TS or ECMAScript. You can use lambda expressions [].forEach((item: any) => console.log(item))

but I'm pretty sure JS or TS doesn't have a wrapper for a lambda expression like Java does.

EDIT: I also found this What does ‘::’ (double colon) do in JavaScript? after a little more searching and it is also a valid ES7 operator as syntactic sugar for bind: http://blog.jeremyfairbank.com/javascript/javascript-es7-function-bind-syntax/

Although it doesn't behave the same way as Java's :: operator.

Community
  • 1
  • 1
Marc Freeman
  • 713
  • 2
  • 7
  • 30