Live data from Hacker News

ECMAScript bind operator proposal

github.com

1–10 of 50 posts

Re: ECMAScript bind operator proposal

#2
Neat! Extension methods and getting rid of "var that = this;" in one!

It seems that it also extends to full expressions on the right-hand side of ::, so you can even do, for using Array operations over things that look like arrays but not quite:

    document.querySelectorAll('p')::([].map)(f)
instead of:

    [].map.call(document.querySelectorAll('p'), f)
(replace [].map with Array.prototype.map if it feels less gross)

Re: ECMAScript bind operator proposal

#4
post #3

With arrow functions released and class properties likely to be approved soon, this has minimal value and is probably not worth the cost of the extra complexity to the language.

Even with arrow functions, it's pretty frequent to have to write things like `...on('event', evt => this.onEvent(evt))` where `on('event', ::onEvent)` would be quite nice.

Re: ECMAScript bind operator proposal

#5
post #2

Neat! Extension methods and getting rid of "var that = this;" in one! It seems that it also extends to full expressions on the right-hand side of ::, so you can even do, for using Array operations over things that look like arrays but not quite: document.querySelectorAll('p')::([].map)(f) instead of: [].map.call(document.querySelectorAll('p'), f) (replace [].map with Array.prototype.map if it feels less gross)

[deleted]

Re: ECMAScript bind operator proposal

#7
post #3

With arrow functions released and class properties likely to be approved soon, this has minimal value and is probably not worth the cost of the extra complexity to the language.

I respectfully disagree.

It all has to do with this line: "By providing syntactic sugar for these use cases we will enable a new class of "virtual method" library, which will have usability advantages over the standard adapter patterns in use today."

Essentially, you write functions that provide a certain behavior without being coupled to a specific class, so you can have a 'map' method that is called the same way no matter what you're mapping over.

Right now functional libraries like static-land, ramda, sanctuary, etc. are working hard to provide these functional primitives but it's still painful to write readable code that uses these.

It's also worth pointing out that directly importing methods here is more efficient than importing entire classes if you're not using one of those emerging build systems that does very good tree-shaking.

This is the closest JavaScript is going to get to letting you write your behavior as a suite of functions separately from specific classes—it's like a step removed from typeclasses.

Re: ECMAScript bind operator proposal

#8
I don't know, I kinda like ".bind(this)" to bind the current scope to a callback.

  class Vector{
  	constructor(x = 0, y = 0, z = 0){
  		this.x = x;
  		this.y = y;
  		this.z = z;
  		
  		setTimeout(function(){
  			console.log(this.x + ", " + this.y + ", " + this.z);
  		}.bind(this), 10);
  	}
  };
  
  var v = new Vector(1, 2, 3); // prints 1, 2, 3
Edit: Ok I can see this be potentially useful for function chaining, I just don't do that very much.

Re: ECMAScript bind operator proposal

#9
post #2

Neat! Extension methods and getting rid of "var that = this;" in one! It seems that it also extends to full expressions on the right-hand side of ::, so you can even do, for using Array operations over things that look like arrays but not quite: document.querySelectorAll('p')::([].map)(f) instead of: [].map.call(document.querySelectorAll('p'), f) (replace [].map with Array.prototype.map if it feels less gross)

I find ".bind(this)" to be preferable over "var that = this" anyway.

Re: ECMAScript bind operator proposal

#10
post #8

I don't know, I kinda like ".bind(this)" to bind the current scope to a callback. class Vector{ constructor(x = 0, y = 0, z = 0){ this.x = x; this.y = y; this.z = z; setTimeout(function(){ console.log(this.x + ", " + this.y + ", " + this.z); }.bind(this), 10); } }; var v = new Vector(1, 2, 3); // prints 1, 2, 3 Edit: Ok I can see this be potentially useful for function chaining, I just don't do that very much.

Your own example is more concisely covered by arrows, which are already available:

  class Vector {
    constructor(x=0, y=0, z=0) {
      this.x = x;
      this.y = y;
      this.z = z;
      setTimeout(() => console.log(this.x + ", " + this.y + ", " + this.z), 10);
    }
  }
Post reply on HN