unit Main;uses aUnit; // makes available all variables in interface section of aUnit
interface
var aGlobal: string; // global in the scope of all units that use Main;typeTmyClass = classstrict private aPrivateVar: Integer; // only known by objects of this class type// lexical: within class definition,// reserved word privatepublic aPublicVar: double; // known to everyboday that has access to a// object of this class typeend;
implementation
var aLocalGlobal: string; // known to all functions following// the definition in this unit
end.
var downloadManager = {initialize: function() {var _this = this; // Set up `_this` for lexical access$('.downloadLink').on('click', function () {_this.startDownload();});},startDownload: function(){this.thinking = true;// Request the file from the server and bind more callbacks for when it returns success or failure}//...};
var downloadManager = {initialize: function() {$('.downloadLink').on('click', function () {this.startDownload();}.bind(this)); // Create a function object bound to `this`}//...
function x() {/*Variable 'a' is only available to function 'x' and function 'y'.In other words the area defined by 'x' is the lexical scope ofvariable 'a'*/var a = "I am a";
function y() {console.log( a )}y();
}// outputs 'I am a'x();
示例2:
function x() {
var a = "I am a";
function y() {/*If a nested routine declares an item with the same name,the outer item is not available in the nested routine.*/var a = 'I am inner a';console.log( a )}y();
}// outputs 'I am inner a'x();
function grandfather() {var name = 'Hammad';// 'likes' is not accessible herefunction parent() {// 'name' is accessible here// 'likes' is not accessible herefunction child() {// Innermost level of the scope chain// 'name' is also accessible herevar likes = 'Coding';}}}
var a='apple',b='banana';
function init() {var a='aardvark',b='bandicoot';document.querySelector('button#a').onclick=function(event) {alert(a);}document.querySelector('button#b').onclick=doB;}
function doB(event) {alert(b);}
init();