Show TOC

JavaScript NamespacesLocate this document in the navigation structure

To avoid conflicts with other frameworks or development, the JavaScript development follows the OpenAJAX concept for namespaces. For this, the sap namespace has been officially reserved for SAP.

All objects, functions, classes, and so on, that have been developed by SAP and are visible globally must either be defined as direct members of that namespace, or as members of one of its subnamespaces. Non-SAPUI5 content, such as application code or controls developed by customers and partners, must not use this sap namespace prefix.

For SAPUI5-related development, the namespace is further restricted to the sap.ui child namespace with an exception for the sap.m entities.

Note

SAP's JavaScript objects with an accessible name that are not part of a closure must have a name starting with the reserved namespace sap. The namespace for SAPUI5 objects must start with sap.ui or sap.m.

Global variables, for example window.xyz, are forbidden. They may lead to conflicts with other frameworks, applications, and so on.

To ease the handling of namespaces, SAPUI5 provides two helper functions

  • jQuery.sap.declare(sModuleName)

  • sap.ui.namespace(sNamespace)

The first helper function declares a module and should be the first statement in any SAPUI5 module. This also ensures that the parent namespace of the main module object, that is, the object with the same name as the module, exists.

The second helper function explicitly ensures the existence of a namespace. A call to this function is only needed when additional namespaces are used in a module. Calling that function instead of simply creating the namespace object makes sense if the object exists already (sap.ui.namespaces checks this first, or if the name hierarchy is deeper and creating/checking the intermediate namespaces becomes cumbersome.

Example
// A module declaration, ensures that sap.ui.sample exists
jQuery.sap.declare("sap.ui.sample.MyClass");

// now it is safe to use that namespace object and assign a new member 'MyClass'
// to it
// Note that jQuery.sap.declare does not create the MyClass object.
// So the developer can decide whether it should be a function, an object,
// or an instance of a specific type
sap.ui.sample.MyClass = { key1 : 'value1' };

// the following line guarantees that <code>sap.ui.sample.subspace</code>
// is a valid object
sap.ui.namespace("sap.ui.sample.subspace");

// now one can use this namespace as well
sap.ui.sample.subspace.member1 = 42;
sap.ui.sample.subspace.member2 = 3.141;
Naming of Subnamespaces (Packages)

Subnamespaces of sap.ui that do not represent classes should have a lowercase name like the Java packages whereas CamelCase should be avoided.

  sap.ui.base
  sap.ui.core
  sap.ui.util