Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26593] Problem with variable scope in SDK 7.5.0GA

GitHub Issuen/a
TypeStory
PriorityNone
StatusClosed
ResolutionDone
Resolution Date2019-07-12T17:21:30.000+0000
Affected Version/sRelease 7.5.0
Fix Version/sn/a
Componentsn/a
Labelsn/a
ReporterAminul Islam
AssigneeUnknown
Created2018-11-22T14:54:24.000+0000
Updated2019-07-12T17:21:30.000+0000

Description

Hello! *Steps to reproduce*: app.js
var axwayExample = true; 
var data = ""; // this works with 7.4.1GA
// data = ""; // this works 7.5.0GA

if (axwayExample) { 
data = "this is global data"; 

} else { 
data = "this is login global data";
}
var platform = Ti.Platform.osname; 
var Window = require('/FirstView');
var win = new Window();
win.open();
FirstView.ja
function FirstView() {
	var self = Ti.UI.createWindow({
		backgroundColor : '#ffffff'
	});
 
	var label = Ti.UI.createLabel({
		color : '#000000',
		text : 'Hello',
		height : 'auto',
		width : 'auto'
	});
	self.add(label);
 
	//Add behavior for UI
	label.addEventListener('click', function(e) {
		alert(data);
	});
 
	return self;
}
module.exports = FirstView;

*Discription*: In 7.4.1 and earlier, any variables defined with the var keyword in app.js were considered as global variables. But it's not working in 7.5.0.GA You can see that to get global variables to work on 7.5.0, the data variable needs to be defined without the var keyword. The main difference in code is that I define the data variable, and set it to an empty string. Then with a if statement, I modify the data variable, but it does not get passed to the alert function in 7.5.0GA unless I do the change as indicated. Test Environment:
Operating System
  Name                        = Microsoft Windows 10 Pro
  Version                     = 10.0.17134
  Architecture                = 32bit
  # CPUs                      = 4
  Memory                      = 17091956736
Node.js
  Node.js Version             = 8.9.1
  npm Version                 = 5.5.1
Titanium CLI
  CLI Version                 = 5.1.1
Titanium SDK
  SDK Version                 = 7.5.0.GA
Thanks

Comments

  1. Ewan Harris 2018-11-22

    [~aislam] The behaviour they have mentioned is deprecated as of 7.5.0, see https://docs.appcelerator.com/platform/latest/#!/guide/Titanium_SDK_7.5.0_Functionality_Update. We transform their code to try and fix the solution for them (for now), but evidently it isn't fully correct when the variable is also used in the app.js file. We will fix that, but I would recommend them to update their code to directly assign to the global scope using global.data = "";

JSON Source