Titanium JIRA Archive
Appcelerator Community (AC)

[AC-4834] Defining var in Alloy.js causes global scope

GitHub Issuen/a
TypeBug
Priorityn/a
StatusResolved
ResolutionInvalid
Resolution Date2017-03-03T14:11:56.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsalloy.js, scope, variables
ReporterCreative
AssigneeShak Hossain
Created2017-03-03T09:58:01.000+0000
Updated2017-03-03T14:11:56.000+0000

Description

Defining a *var* in alloy.js actually creates a "global" variable since the context is the app itself. One would expect to have any local variable here go out of scope.
// File: app/alloy.js
var mytestvar = 'foobar';
Next, open any controller or common.js module, and simply retrieve the variable using *console.log(mytestvar);*. It will exist forever during the app's runtime. I know one solution is to simply put alloy.js in a *(function(){})()* wrapper but the current implementation seems wrong by design.

Comments

  1. Hans Knöchel 2017-03-03

    This is made by design. The alloy.js is meant to be global if needed (although not recommended). People usually store global modules instances like Ti.Map there.

JSON Source