-
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
Binding new
?
#25
Comments
This could be a sweet addition. Being able to |
Thanks @IMPinball ! After @dtinth brought up Java method references over in #18, I considered this as well. I think it would make a lot of sense provided that we split up this proposal into separate "method extraction" and "pipelining" operators. This would fit right in with a method extraction operator. console::log; // => (...args) => console.log(...args) // or something like that
Array::new; // => (...args) => new Array(...args) // or something like that |
👍 Referencing new (and methods) like this is super useful and pairs well with classes, especially when deserializing. |
@ssube "deserializing"? Can you explain? |
@zenparsing Instantiating classes with data, like |
@ssube Gotcha. |
See #26. |
Rather than request to new syntax, we can just implement it under current frame. class Foo {}
Foo.new( 'foo' );
array.map( Foo.new );
'foo' |> Foo.new; Before proposal goes, we can just only do prototype modification to implement it. Reflect.defineProperty( Function.prototype, 'new', {
get(){
const newer= ( ...args )=> new this( ...args, );
Reflect.defineProperty( newer, 'length', { value:this.length, }, );
Reflect.defineProperty( newer, 'name', { value:`${this.name}.new`, }, );
return newer;
},
}, ); |
Could you please not resurrect an issue closed 4 years ago for this? It's not helpful and I'd rather not deal with any more notification spam than I have to. |
The current syntax seems like it would work well with auto-binding constructors as well. Java also has a version for this, with its
Class::new
shorthand. The same thing could be very useful for JavaScript as well, since classes can't be instantiated withoutnew
.Only incidentally, I'm suggesting a very similar syntax. Since
new
is a keyword in all contexts, it couldn't possibly be an identifier for abound
function, so it would work very well.As for the suggested semantics,
expr::new
should function identically to the following when called:More specific semantics:
Syntax:
Runtime Semantics: BoundConstructorCreate(target, boundArgs)
Note: This is equivalent BoundFunctionCreate(target, null, empty List) where target is always a constructor, but with the exception that [[Call]] and [[Construct]] have the same implementation.
Runtime Semantics:
The text was updated successfully, but these errors were encountered: