ECMAScript bind operator proposal
github.com
ECMAScript bind operator proposal
1–10 of 50 posts
Re: ECMAScript bind operator proposal
#2It 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
#3Re: ECMAScript bind operator proposal
#4With 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.
Re: ECMAScript bind operator proposal
#5Neat! 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
#6https://github.com/jussi-kalliokoski/trine
Re: ECMAScript bind operator proposal
#7With 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.
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 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
#9Neat! 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
#10I 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.
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);
}
}