-
Notifications
You must be signed in to change notification settings - Fork 1
LangSpecs Export
Flatscript output Javascript within an anonymous Javascript function so that even global name definitions are actually local, which keeps all the name from being visible to other module in browser environment.
To export names to the outside world, export statements are needed. export statements are only allowed in a non-regular-asynchonous global scope.
Syntax is
export dot-separated-identifier-list : expression
where dot-separated-identifier-list is a list of identifiers that contains at least one identifier, with dot between each two successive identifiers.
For example
export foo: 123
export bar.baz: 34
An export statement is not a name definition so what it exports is not available in the scope. For example
# Beginning of the file
export foo: 123
console.log(foo) # error: foo is not defined
In the browser environment, an export statement result in that a property of window is set. For example
export foo: 123
export bar.baz: 34
is equivalent to Javascript
window.foo = 123;
window.bar = {};
window.bar.baz = 34;
In the nodejs environment, it set a property to exports. For example the Flatscript code above will result in
exports.foo = 123;
exports.bar = {};
exports.bar.baz = 34;
In the included file, an export statement provides a name that is available as a module property.
For example, in "a.fls"
export foo: 123
export bar.baz: 34
in "b.fls"
include 'a.fls' as a
console.log(a.foo) # 123
console.log(a.bar.baz) # 34
The export keyword could be prepend to a class definition or a function definition. For example
export class Bar
ctor()
this.bar: 34
export func baz()
return 1234
they are equivalent to
class Bar
ctor()
this.bar: 34
export Bar: Bar
func baz()
return 1234
export baz: baz
Unlike value export statements, the class name or function name exported in this way is available in the scope.
A command line option -p or --export-point is used to change the default export point from window to its property. For example, with -p foo
export bar: 123
export func baz(x, y)
return x * y
is equivalent to Javascript
window.foo = {};
window.foo.bar = 123;
window.foo.baz = function(x, y) {
return x * y;
};