external

Summary

Imports symbols from JavaScript. If declared with an initializer, the external keyword declares a variable with type external and initialized to an arbitrary value.

Syntax

external name1[= value1 [, name2 [, name3 ... [, nameN]]]];

Parameters

nameN
The name of the symbol to import from JavaScript. This can be any valid JavaScript identifier. If an initializer is present, no importing will happen and this will simply act as the name to assign to the initialized value.
valueN
Optional. Initializes the name to a value.

Description

The external keyword will import symbols from JavaScript. However, if the external keyword is used with an initializer, it will declare a variable of type external instead.

The external keyword can only be used at the top-level scope of a file or inside a module. Note that there is a difference between externals declared at the program level (top-level scope) and externals declared at the module level. Externals declared at the program level are scoped to the file, while externals declared inside a module will be exported with the module. Thus, modules that wish to utilize externals without exporting them should have the externals declared at the program level scope of the module file.

In addition, the external keyword cannot be modified after its declaration. In order to declare a variable with the external type but which can later be modified, use a non-final var.

Examples

Importing jQuery
1
2
3
4
// jQuery declares two symbols: 'jQuery' and '$' so we import both
external jQuery, $;
 
$("#myelement").hide();
Importing Node.js Modules
1
2
3
4
5
6
7
8
9
10
// Import 'require' first
external require;
// After 'require' has been imported, we can use it to import the 'fs' module
external fs = require("fs");
 
fs.writeFile("message.txt", "Hello World", void(err) {
    if (err) {
        throw err;
    }
});
jQuery AJAX Example
1
2
3
4
5
6
7
8
9
10
11
// jQuery declares two symbols: 'jQuery' and '$' so we import both by convention
external jQuery, $;
 
$.ajax({
    url: "myfile.txt",
    success: void(string result) {
        result = "#BEGIN#" + result + "#END#";
         
        $("#myelement").text(result);
    }
});

See Also

Share

HTML | BBCode | Direct Link