You are on page 1of 18

JavaScript for ABAP Programmers

Chapter 4.2 - Variable Hoisting


Chris Whealy / Technology RIG
ABAP JavaScript
Data typing Strong Weak
Syntax Similar to COBOL Derived mainly from Java with some influence from C
Variable Scope Block scope Lexical (or function) scope
“Function” No concept of a function as a Functions are 1st class citizens.
Concept data object That is, a function is an object with “an executable part”
Inheritance Model Class based Referential (or Prototypical)
Programming Style Strictly Imperative Both Imperative and Functional
Variable Hoisting
Variable Declarations are Hoisted
Properties are Declared Sequentially
Consequences of Hoisting
Variable Declarations Are Hoisted! 1/5

Understanding the difference between variables and properties is important because it has an impact
on the way you should write your code.
A variable is any named value declared using the var keyword.

© 2015 SAP AG. All rights reserved. 4


Variable Declarations Are Hoisted! 2/5

Understanding the difference between variables and properties is important because it has an impact
on the way you should write your code.
A variable is any named value declared using the var keyword.
When an execution context starts (I.E. a function is called), JavaScript scans the source code in that
execution context looking for any use of the var keyword (I.E. variable declarations). Any variables it
finds have their names defined immediately – before any statements of that function are executed.

© 2015 SAP AG. All rights reserved. 5


Variable Declarations Are Hoisted! 3/5

Understanding the difference between variables and properties is important because it has an impact
on the way you should write your code.
A variable is any named value declared using the var keyword.
When an execution context starts (I.E. a function is called), JavaScript scans the source code in that
execution context looking for any use of the var keyword (I.E. variable declarations). Any variables it
finds have their names defined immediately – before any statements of that function are executed.
However, the variable will not be assigned a value until execution reaches that statement!

© 2015 SAP AG. All rights reserved. 6


Variable Declarations Are Hoisted! 4/5

Understanding the difference between variables and properties is important because it has an impact
on the way you should write your code.
A variable is any named value declared using the var keyword.
When an execution context starts (I.E. a function is called), JavaScript scans the source code in that
execution context looking for any use of the var keyword (I.E. variable declarations). Any variables it
finds have their names defined immediately – before any statements of that function are executed.
However, the variable will not be assigned a value until execution reaches that statement!
// Doing things backwards, but getting away with it (kinda)
alert(someNumber); //  undefined. Because of hoisting the variable name is known, but has no value yet

var someNumber = 10; // Now assign it a value

alert(someNumber); //  10

© 2015 SAP AG. All rights reserved. 7


Variable Declarations Are Hoisted! 5/5

Understanding the difference between variables and properties is important because it has an impact
on the way you should write your code.
A variable is any named value declared using the var keyword.
When an execution context starts (I.E. a function is called), JavaScript scans the source code in that
execution context looking for any use of the var keyword (I.E. variable declarations). Any variables it
finds have their names defined immediately – before any statements of that function are executed.
However, the variable will not be assigned a value until execution reaches that statement!
// Doing things backwards, but getting away with it (kinda)
alert(someNumber); //  undefined. Because of hoisting the variable name is known, but has no value yet

var someNumber = 10; // Now assign it a value

alert(someNumber); //  10

This situation is known as "hoisting" because JavaScript behaves is as if all variable declarations are
lifted (or hoisted) to the top of the function’s source code.

© 2015 SAP AG. All rights reserved. 8


Properties Are Declared Sequentially! 1/4

A property is created simply by assigning a value to a name. However, unlike variable name
declarations, properties are not hoisted. Their creation takes place in a strictly sequentially manner.
The property name will remain unknown to JavaScript until execution reaches the statement.
Depending on how the property is referenced, this might result in a runtime reference error.

// Doing things backwards and not always getting away with it


alert(window.someProperty); //  undefined. The window object is known, but the property name isn't

© 2015 SAP AG. All rights reserved. 9


Properties Are Declared Sequentially! 2/4

A property is created simply by assigning a value to a name. However, unlike variable name
declarations, properties are not hoisted. Their creation takes place in a strictly sequentially manner.
The property name will remain unknown to JavaScript until execution reaches the statement.
Depending on how the property is referenced, this might result in a runtime reference error.

// Doing things backwards and not always getting away with it


alert(window.someProperty); //  undefined. The window object is known, but the property name isn't
alert(someProperty); //  KABOOM! (Reference error) The unqualified property name is unknown!

© 2015 SAP AG. All rights reserved. 10


Properties Are Declared Sequentially! 3/4

A property is created simply by assigning a value to a name. However, unlike variable name
declarations, properties are not hoisted. Their creation takes place in a strictly sequentially manner.
The property name will remain unknown to JavaScript until execution reaches the statement.
Depending on how the property is referenced, this might result in a runtime reference error.

// Doing things backwards and not always getting away with it


alert(window.someProperty); //  undefined. The window object is known, but the property name isn't
alert(someProperty); //  KABOOM! (Reference error) The unqualified property name is unknown!

someProperty = 10; // This statement both creates the global property and assigns it a value

© 2015 SAP AG. All rights reserved. 11


Properties Are Declared Sequentially! 4/4

A property is created simply by assigning a value to a name. However, unlike variable name
declarations, properties are not hoisted. Their creation takes place in a strictly sequentially manner.
The property name will remain unknown to JavaScript until execution reaches the statement.
Depending on how the property is referenced, this might result in a runtime reference error.

// Doing things backwards and not always getting away with it


alert(window.someProperty); //  undefined. The window object is known, but the property name isn't
alert(someProperty); //  KABOOM! (Reference error) The unqualified property name is unknown!

someProperty = 10; // This statement both creates the global property and assigns it a value

alert(someProperty); //  10

© 2015 SAP AG. All rights reserved. 12


Consequences of Hoisting 1/5

Due to the fact that all JavaScript variables are hoisted, the location of a variable’s declaration within
the source code of a function is not important.

function test() {
var a = 10;
Variable a is both declared and assigned
a value in a single statement.
for (var i=0; i<5; i++) {
console.log("a + i = " + (a + i));
}
}

© 2015 SAP AG. All rights reserved. 13


Consequences of Hoisting 2/5

Due to the fact that all JavaScript variables are hoisted, the location of a variable’s declaration within
the source code of a function is not important.

function test() {
var a = 10;

for (var i=0; i<5; i++) {


console.log("a + i = " + (a + i));
} This gives the expected
} runtime results

© 2015 SAP AG. All rights reserved. 14


Consequences of Hoisting 3/5

However due to the effects of hoisting, the location of the first assignment to a variable could have
serious consequences for the logic of your coding!

function test() {
for (var i=0; i<5; i++) {
console.log("a + i = " + (a + i));
}
Moving the declaration to the end of the
var a = 10;
} function has no impact on whether JavaScript
“knows” the name of this variable…

© 2015 SAP AG. All rights reserved. 15


Consequences of Hoisting 4/5

However due to the effects of hoisting, the location of the first assignment to a variable could have
serious consequences for the logic of your coding!

function test() { However, the assignment of the variable’s


for (var i=0; i<5; i++) { value has also been moved.
console.log("a + i = " + (a + i)); The logic of the coding is now broken!
}

var a = 10;
}

© 2015 SAP AG. All rights reserved. 16


Consequences of Hoisting 5/5

However due to the effects of hoisting, the location of the first assignment to a variable could have
serious consequences for the logic of your coding!

function test() {
for (var i=0; i<5; i++) {
console.log("a + i = " + (a + i));
}

var a = 10;
}

IMPORTANT!
Even though JavaScript knows about all the declared variables in a function before the first statement
of the function is executed, the value of those variables will always be undefined until the flow of
execution reaches a statement that assigns the variable a value!

© 2015 SAP AG. All rights reserved. 17


© 2015 SAP AG. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express Google App Engine, Google Apps, Google Checkout, Google Data API, Google Maps, Google Mobile Ads,
permission of SAP AG. The information contained herein may be changed without prior notice. Google Mobile Updater, Google Mobile, Google Store, Google Sync, Google Updater, Google Voice,
Google Mail, Gmail, YouTube, Dalvik and Android are trademarks or registered trademarks of Google Inc.
Some software products marketed by SAP AG and its distributors contain proprietary software components of
other software vendors. INTERMEC is a registered trademark of Intermec Technologies Corporation.
Microsoft, Windows, Excel, Outlook, PowerPoint, Silverlight, and Visual Studio are registered trademarks of Wi-Fi is a registered trademark of Wi-Fi Alliance.
Microsoft Corporation.
Bluetooth is a registered trademark of Bluetooth SIG Inc.
IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System
Motorola is a registered trademark of Motorola Trademark Holdings LLC.
z10, z10, z/VM, z/OS, OS/390, zEnterprise, PowerVM, Power Architecture, Power Systems, POWER7,
POWER6+, POWER6, POWER, PowerHA, pureScale, PowerPC, BladeCenter, System Storage, Storwize, Computop is a registered trademark of Computop Wirtschaftsinformatik GmbH.
XIV, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, AIX, Intelligent Miner, WebSphere, Tivoli,
Informix, and Smarter Planet are trademarks or registered trademarks of IBM Corporation. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer, StreamWork,
SAP HANA, and other SAP products and services mentioned herein as well as their respective logos are
Linux is the registered trademark of Linus Torvalds in the United States and other countries. trademarks or registered trademarks of SAP AG in Germany and other countries.
Adobe, the Adobe logo, Acrobat, PostScript, and Reader are trademarks or registered trademarks of Adobe Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web
Systems Incorporated in the United States and other countries. Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their
respective logos are trademarks or registered trademarks of Business Objects Software Ltd. Business Objects
Oracle and Java are registered trademarks of Oracle and its affiliates.
is an SAP company.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, and other Sybase products and
Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or services mentioned herein as well as their respective logos are trademarks or registered trademarks of Sybase
registered trademarks of Citrix Systems Inc. Inc. Sybase is an SAP company.
HTML, XML, XHTML, and W3C are trademarks or registered trademarks of W3C®, World Wide Web Crossgate, m@gic EDDY, B2B 360°, and B2B 360° Services are registered trademarks of Crossgate AG
Consortium, Massachusetts Institute of Technology. in Germany and other countries. Crossgate is an SAP company.
Apple, App Store, iBooks, iPad, iPhone, iPhoto, iPod, iTunes, Multi-Touch, Objective-C, Retina, Safari, Siri, All other product and service names mentioned are the trademarks of their respective companies. Data
and Xcode are trademarks or registered trademarks of Apple Inc. contained in this document serves informational purposes only. National product specifications may vary.
IOS is a registered trademark of Cisco Systems Inc. The information in this document is proprietary to SAP. No part of this document may be reproduced, copied,
or transmitted in any form or for any purpose without the express prior written permission of SAP AG.
RIM, BlackBerry, BBM, BlackBerry Curve, BlackBerry Bold, BlackBerry Pearl, BlackBerry Torch, BlackBerry
Storm, BlackBerry Storm2, BlackBerry PlayBook, and BlackBerry App World are trademarks or registered
trademarks of Research in Motion Limited.

© 2015 SAP AG. All rights reserved. 18

You might also like