Reputation: 10645
I'm trying to create an object called $browser
within my javascript namespace which returns Internet Explorer version numbers based on feature detection.
I'm having trouble calling the function ieVersion
within my object and have got the error ieVersion is not defined
.
I can call $browser.ieVersion()
outside the $browser
object but not within.
what i'd like to be able to do is say ..
if($browser.ie6){
// run my specific code...
}
Any pointers.....
UPDATE.... I've tried to follow the advice from Casablanca and have updated the code to reflect my interpretation of his changes.... I now have the error Expected ':'
at Namespace.$browser
My Code....
// Namespace the functions to remove possibility of conflict.
var Namespace = {
/// <summary>
/// The browser object allows detection of ie versions.
/// </summary>
$browser: {
ie6: Namespace.$browser.ieVersion() === 6,
/// <summary>
/// Uses feature detection to return the internet explorer browser number.
/// </summary>
ieVersion: function () {
var $version = 0;
// The browser is IE 6 - 8.
if (!jQuery.support.leadingWhitespace) {
// IE 6 & 7.
if (!jQuery.support.boxModel) {
if (!jQuery.support.opacity && !window.XMLHttpRequest) {
$version = 6;
}
else {
$version = 7;
}
}
else {
$version = 8;
}
}
return $version;
}
},
Namespace.$browser.ie6 = Namespace.$browser.ieVersion() === 6;
};
Upvotes: -1
Views: 115
Reputation: 70721
There are two problems with this line:
ie6: ieVersion() === 6
First, ieVersion
by itself refers to the global (rather function-level) namespace. You need to fully qualify it as Namespace.$browser.ieVersion
.
Second, ieVersion
isn't defined yet. It is available only after the entire object has been created. You cannot refer to another function within the same object literal. Thus you can only initialize the ie6
property after the entire object has been defined.
Put this line below the declaration of Namespace
:
Namespace.$browser.ie6 = Namespace.$browser.ieVersion() === 6;
Upvotes: 1