-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't we just have a simple object::method binding syntax? #54
Comments
They achieve different purposes; the bind operator has 3 use cases, 2 of which pipeline solves - method extraction is the one it doesn't solve, which will need to be addressed somehow. |
If only const wm = new WeakMap;
const create = obj => {
const map = new Map;
wm.set(obj, map);
return map;
};
const set = (obj, method) => {
const map = wm.get(obj) || create(obj);
const bound = method.bind(obj);
map.set(method, bound);
return bound;
};
const bound = (obj, method) => {
const map = wm.get(obj);
return map && map.get(method) || set(obj, method);
}; So that |
@WebReflection I agree we need to address this pain point. Syntactically speaking, binary |
Nothing different here with following python code: class A:
def f(): pass
a = A()
print(a.f is a.f) # False I don't think there are any importance for |
@tiansh I guess that's because you've never added a listener you want remove later on obj.addEventListener('type', obj::method); That is why is important, many developers write I've solved this via About the pipeline VS method extraction, I've created an utility that does something similar to this: Function.prototype.this = function () {
return self => this.apply(self, arguments);
}; That plays super nicely with pipeline operator: const {map, sort} = Array.prototype;
const names = document.querySelectorAll('*')
|> map.this(el => el.nodeName)
|> sort.this(); If that could be proposed in core, it'd be awesome. |
@WebReflection |
You really don't have to explain me anything, developers keep doing that mistake, not me, never me. I use handleEvent or cached listeners already. |
Yes, that’s because bad api design. Not by language. And this (use a uncatched right value for removeEventListener) can be easily detected by browsers and linters. But not by language. |
@tiansh In python, bound methods do not have object identity but they do have equality, which makes a difference: class A:
def f(): pass
a = A()
d = dict([(a.f, 1)])
a.f in d # True |
This would be similar to the Java 8 method reference syntax, which I consider a plus: Consumer<String> println = System.out::println;
println.accept("Some string");
// Equivalent to: (Anonymous object implementation)
var println = new Consumer<>() {
public void accept(String string) {
System.out.println(string);
}
};
// And to: (Java 8 lambda syntax)
Consumer<String> println = string -> System.out.println(string); Uses the |
Having const sym = Symbol("extracted");
Object.prototype.extract = function (method) {
if (!this[sym]) {
this[sym] = new WeakMap();
}
if (!this[sym][method]) {
this[sym][method] = method.bind(this);
}
return this[sym][method];
}; |
Nothing new can be added to Object.prototype; and that wouldn’t address null objects anyways. |
@ljharb this method is supposed to represent the |
in that case, storing the cache observably and mutably on the object - which might be frozen anyways - is a nonstarter. |
the solution for equality was already posted here and it's still not difficult: this proposal is going nowhere anyway, so I think it could be closed? |
That's hitting the real pain point. Binding a method from third part library can be substituted by There is a plan to mix them together. function addClass( className ){ this.classList.add( className ); }
const foo= document.getElementById( 'foo' );
const setAttributeToFoo= foo::setAttribute;
const removeAttributeFromFoo= foo::['removeAttribute'];
const addClassToFoo= foo::{addClass}; |
Why are you trying to make a simple thing complicated? All we need is sugar to:
Drop the pipeline non-sense.
The text was updated successfully, but these errors were encountered: