-
Notifications
You must be signed in to change notification settings - Fork 48
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
export keyword #121
Comments
You mean, the global object is frozen by default in that environment or something? Then you shouldn't be able to add global variables at all. Running
on Firefox 11 gives Error: a is read-only. |
In strict mode, you can't use this inside a function that's not a property of an object or called with the call method. The compiler wraps all your code in a function. Using the bare option defeats the safeguards. Edit: Fixed |
That's why we use |
@satyr
Yes, it can be gotten around, but there are times when your environment does stupid things... |
How?
Depends on how |
Well, you have to remove the line marked #wrong to get the error. It's an "undeclared variable" error. And I don't want to call e with funny calls every time, or bind every function in my code. I think an export would be a better solution, it's much less coding. |
Assuming your intention at
is what you want. |
Isn't that rather verbose? And the issue is when I want to do that with a function. I would have to opt for the much less readable global.f = function ... for every function in my code which needs to be put on the global object. |
On top level that's merely |
I'm not always going to be on the top level, if I want to reference it, I have to make the global object a variable, or call all my functions using .call @
I think Edit: Fixed. |
Try |
The I'm aware you don't like using multiple files and joining them. But we don't all live in a node.js environment where we can |
Ah, hoisting. In that case you do need |
Looks good. Though I would make |
I would personally rather have Currently I'm using a modified version of CoffeeScript where macro func 'export', (expr) -> assign(
objAccess (value 'exports'),
if expr instanceof Class then expr.determineName()
else if expr instanceof Assign then expr.variable
else if expr instanceof Value then expr
else throw new Error 'Cannot determine export name'
expr
) $ bin/coffee -bep 'export foo = (a,b) -> c'
var foo;
exports.foo = foo = function(a, b) {
return c;
}; $ bin/coffee -bep 'export bar'
exports.bar = bar; $ bin/coffee -bep 'export class Baz'
var Baz;
exports.Baz = Baz = (function() {
function Baz() {}
return Baz;
})(); I've been using that for some time now and its been working out quite well for me. |
We can further define:
The top level |
Is it possible to make the whole function return the exported variables? For when some environments use eval on your script... |
Like how? |
Faster way might be to make the code return the "this" object, but that might cause problems. |
Too bombastic for a relatively narrow use case. I guess you can just add |
And for the record, one QtScript environment I script for DOES use eval on your script. It's stupid, I know. They all seem to be stupid somehow, seems to be a thing with programs which use QtScript. Edit: Just realised this wont work, since there's no way to put the "use return_exports" at the top of the script for sure... |
It does sound like hard to make QtScript code reusable in browser/CommonJS. Maybe you can abuse hoisting:
|
But than the constructor won't be accessible in scopes with different |
Hm, this at-name linking is kinda annoying.
It will:
|
I was wondering if it'd be possible to add an export keyword. The reason is that in some JavaScript environments (hint hint, QtScript) people tend to require certain variable on the global scope. However, with the QtScript transition to V8, adding them to the "this" property will become a strict mode error if you don't use .call on a method, furthermore, many implementations don't posses a window-like object for the global scope.
The solution is to use the bare mode, but this isn't desirable. Instead I would propose the use of a special keyword, export.
Might compile to:
The text was updated successfully, but these errors were encountered: