var abc = abc || {}; in Java Script
-
Hi All, I am having a script file which begins with the following statement:
var ABC = ABC || {};
This is the very first line of the Java Script file, while the remaining is understandable. Can anyone please tell me what does this line means. I am unable to understand the meaning of it Thanks
-
Hi All, I am having a script file which begins with the following statement:
var ABC = ABC || {};
This is the very first line of the Java Script file, while the remaining is understandable. Can anyone please tell me what does this line means. I am unable to understand the meaning of it Thanks
Let me reply my Question: The following line creates namespace in Java script:
var ABC = ABC || {};
It says if there is a class called ABC create a namespace of it, otherwise create a blank namespace. Hope this may help others for understanding.
-
Let me reply my Question: The following line creates namespace in Java script:
var ABC = ABC || {};
It says if there is a class called ABC create a namespace of it, otherwise create a blank namespace. Hope this may help others for understanding.
Umair Feroze wrote:
It says if there is a class called ABC create a namespace of it, otherwise create a blank namespace.
No, it doesn't create a namespace, because JavaScript doesn't have namespaces; and it does not look for a class as JavaScript does not have classes. It leaves abc as it is if it is an expression whose Boolean value is true e.g. is a non-zero value, a non-empty string, true, a function, or an object (including an array, even if empty). If it is not a true value, it replaces it with an empty object. This is a common idion for defining a variable to hold a context (not a JavaScript concept) without accidentally overwriting it if it is already in use. It is better than simply doing
var abc = { };
because any properties in the object will be retained. Technically, it is bad JavaScript becausevar
creates a variable. The appearance of it on the right hand side of an expression when defining it in the left hand side could (in theory) end up using it between the time it is defined and the time it is initialised. I'd rather do ...if (typeof abc != 'object') // Use existing abc if it is an object
var abc = { }; // Create empty abcalthough it is questionable as to whether this is valid or not as
var
s are 'hitched' so it is equivalent tovar abc;
if (typeof abc != 'object')
abc = { };but you can get away with it because varing a variable that already exists in the current scope has no effect.
-
Umair Feroze wrote:
It says if there is a class called ABC create a namespace of it, otherwise create a blank namespace.
No, it doesn't create a namespace, because JavaScript doesn't have namespaces; and it does not look for a class as JavaScript does not have classes. It leaves abc as it is if it is an expression whose Boolean value is true e.g. is a non-zero value, a non-empty string, true, a function, or an object (including an array, even if empty). If it is not a true value, it replaces it with an empty object. This is a common idion for defining a variable to hold a context (not a JavaScript concept) without accidentally overwriting it if it is already in use. It is better than simply doing
var abc = { };
because any properties in the object will be retained. Technically, it is bad JavaScript becausevar
creates a variable. The appearance of it on the right hand side of an expression when defining it in the left hand side could (in theory) end up using it between the time it is defined and the time it is initialised. I'd rather do ...if (typeof abc != 'object') // Use existing abc if it is an object
var abc = { }; // Create empty abcalthough it is questionable as to whether this is valid or not as
var
s are 'hitched' so it is equivalent tovar abc;
if (typeof abc != 'object')
abc = { };but you can get away with it because varing a variable that already exists in the current scope has no effect.
jsc42 wrote:
because JavaScript doesn't have namespaces
Not quite correct. Namespaces in JavaScript[^], Namespacing your JavaScript[^], The Importance of Namespace (and a JavaScript solution)[^] plus many other resources.
I know the language. I've read a book. - _Madmatt
-
jsc42 wrote:
because JavaScript doesn't have namespaces
Not quite correct. Namespaces in JavaScript[^], Namespacing your JavaScript[^], The Importance of Namespace (and a JavaScript solution)[^] plus many other resources.
I know the language. I've read a book. - _Madmatt
Defining variables to hold other methods and properties to minimise the pollution of the global scope is a very well established technique. This technique emulates some of the key features of namespaces in other languages. I use this method in my own code and add comments saying that it is creating a namespace; but that does not make it a namespace, any more that if I commented it as being a Space Shuttle would make it a Space Shuttle; they are still just variables that have properties attached to them. Compare, in some fictitious language that has 'real' namespaces:
namespace Namespace1
{
helloWorld: 'Hello, World'
}alert(Namespace1.helloWorld); // Valid
Namespace1 = false; // Invalid. A namespace is not a variable, so it cannot be given a value
alert(Namespace1.helloWorld); // Still valid
against JavaScript:
/* Cannot use namespace as it is not a keyword
namespace Namespace1
*/
// Work around ... use a variable
var Namespace1 =
// End of work around
{
helloWorld: 'Hello, World'
}alert(Namespace1.helloWorld); // Valid
Namespace1 = false; // Valid. This is just a a variable, so it can be overwritten
alert(Namespace1.helloWorld); // Was valid before, but is no longer valid.
-
Defining variables to hold other methods and properties to minimise the pollution of the global scope is a very well established technique. This technique emulates some of the key features of namespaces in other languages. I use this method in my own code and add comments saying that it is creating a namespace; but that does not make it a namespace, any more that if I commented it as being a Space Shuttle would make it a Space Shuttle; they are still just variables that have properties attached to them. Compare, in some fictitious language that has 'real' namespaces:
namespace Namespace1
{
helloWorld: 'Hello, World'
}alert(Namespace1.helloWorld); // Valid
Namespace1 = false; // Invalid. A namespace is not a variable, so it cannot be given a value
alert(Namespace1.helloWorld); // Still valid
against JavaScript:
/* Cannot use namespace as it is not a keyword
namespace Namespace1
*/
// Work around ... use a variable
var Namespace1 =
// End of work around
{
helloWorld: 'Hello, World'
}alert(Namespace1.helloWorld); // Valid
Namespace1 = false; // Valid. This is just a a variable, so it can be overwritten
alert(Namespace1.helloWorld); // Was valid before, but is no longer valid.
You are comparing apples and oranges. What is your point? You made the statement "because JavaScript doesn't have namespaces", to which I replied, "Not quite correct". I did not say it does have namespaces, but rather a facsimile within the constraints of the language. To say it does not, or does not support the construct, is false and misleading.
I know the language. I've read a book. - _Madmatt