-
Notifications
You must be signed in to change notification settings - Fork 6
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
Why is the way 'this' passed was in different way in NamespaceExtension and ConstructorExtension? #6
Comments
They are designed for common use cases. For constructors, the common use cases are reusing the prototype method. For namespace objects, the common cases are using the first argument as About your example code, it actually works (with some syntax correction). // file a.js
export function A(self) {
console.log(self.a)
}
// file b.js
// import { ::A } from 'a.js' // note this is the wrong syntax as current draft
import ::{A} from 'a.js' // this is the correct syntax as current draft
import * as a from 'a.js'
const obj {
a: 'hello'
}
obj::a:A() // this would work
obj::A() // this actually works as current draft! Note as current draft: import {A} from 'a.js'
const ::A = A
obj::A() // wrong usage! |
Do If it isn't, then these two statement won't have the same semantic or be interchangeable? Besides that, Is expose of namespace object intend to make pattern like this possible? const encoder = new Encoder()
const result = input::encoder:encode(encodeOptions); |
Yes. // module m
export function plus1(x) { return x + this.one }
export const one = 1 import ::{plus1} from 'm'
1::plus1() // 2 But I don't think programmers would write code like that. Of coz there is possibility someone really want to export a free method, in that case, they should use
Assume |
This sounds like a terribly confusing “feature” to me. I would expect In my opinion, this syntax: import ::{ A } from 'a'; should just be a shorthand for this syntax: import { A } from 'a';
const ::A = A; |
How about the reverse? Which means. // a
export function A () {
console.log(this)
}
// b
import ::{ A } from 'a';
import * as a from 'a';
import { A as A1 } from 'a';
const ::A1 = A1
"a"::a:A()
"a"::A()
"a"::A1() Either of them get the same result It looks giantly unnatural to me that
And this can also make tool chains happy due to:
|
Looks like the ways arguments was passed will be altered depends on how you organize the extension?
Besides that.
The
ConstructorExtension
don't expose the object the method was on (a.k.a.ext.prototype
) to the method.While
NamespaceExtension
by default do that (ext
itself was called asthis
).Is this ever required?
Shouldn't the method be able to just refer the object that host it as
ext
(or whatever) if it must do it?And this breaks compatibility of namespace import and named import
That would makes refactor from namespace import to named import require a complete code rewrite because it breaks the semantic.
The text was updated successfully, but these errors were encountered: