简体   繁体   English

Javascript对象请帮忙

[英]Javascript objects help please

I'm trying to create an object called $browser within my javascript namespace which returns Internet Explorer version numbers based on feature detection. 我试图在我的JavaScript名称空间中创建一个名为$browser的对象,该对象根据功能检测返回Internet Explorer版本号。

I'm having trouble calling the function ieVersion within my object and have got the error ieVersion is not defined . 我在对象中调用函数ieVersion遇到麻烦,并且遇到错误ieVersion is not defined的错误。

I can call $browser.ieVersion() outside the $ browser object but not within. 我可以在$ browser对象外部调用$browser.ieVersion() ,但不能在内部调用。

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 更新...。我试图遵循卡萨布兰卡的建议,并更新了代码以反映我对他所做更改的解释....我现在在Namespace.$browser处出现错误Expected ':' 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;    

};

There are two problems with this line: 这条线有两个问题:

ie6: ieVersion() === 6

First, ieVersion by itself refers to the global (rather function-level) namespace. 首先, ieVersion本身是指全局(而不是函数级)命名空间。 You need to fully qualify it as Namespace.$browser.ieVersion . 您需要将其完全限定为Namespace.$browser.ieVersion

Second, ieVersion isn't defined yet. 其次, ieVersion尚未定义。 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. 因此,只能在定义整个对象后初始化ie6属性。

Put this line below the declaration of Namespace : 将此行放在Namespace的声明下面:

Namespace.$browser.ie6 = Namespace.$browser.ieVersion() === 6;

It's telling you that this line: 它告诉您这一行:

Namespace.$browser.ie6 = Namespace.$browser.ieVersion() === 6;  

is not a valid property of your browser object, which is where you've put it. 不是您的浏览器对象的有效属性,这是您放置它的位置。 It should have a : instead of an = (though the syntax would still be invalid since you can't namespace your property name) or it should be placed outside of the object declaration. 它应该有一个:而不是= (尽管语法仍然无效,因为您无法为属性名称命名空间),或者应将其放在对象声明之外。

Also, you still have the exact same problem as mentioned by @casablanca earlier in the code -- even though you namespaced the code in your ie6 property it's still referencing a function that doesn't exist yet. 此外,您仍然遇到与代码中前面提到的@casablanca完全相同的问题 - 即使您在ie6属性中命名代码,它仍然引用一个尚不存在的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM