为什么我不能在我的js函数中访问全局变量?

时间:2013-10-18 09:44:25

标签: javascript jquery function scope

我正在尝试在我的js应用程序脚本中设置一组全局变量,这些变量允许我在页面和网站的整个功能中访问它们。出于某种原因,即使我知道对象在那里,我仍然在我的控制台中得到未定义。

这是我的js片段,js很长,所以我想我会告诉你重要的一点是错的(我认为)

(function ($) {

    "use strict";

    var global = function() {
        this.init();
    };

    global.prototype = {

        // ------------------------------------
        // Global variables

            mainContainer : 'div#container',
            tmbContainer : '.rsThumbsContainer',
            tmbContainerT : '.rsThumbsContainer',
            slider : '.collection #gallery-t-group',
            body : 'body',
            close : '<div class="close-button" id="close"><p class="icon-close"></p></div>',
            socials : '.socialbar-vertical',
            loader : '<div class="loader"></div>',
            gallery : '.collection #gallery-t-group',


        // ------------------------------------
        // Initialise

        init: function() {

            var app = this;

            this.testGlobals();
            this.loadSlide();
            this.fakingIt();
            this.unloadSlide();
            this.mobileNav();
            this.loadThumbs();
            this.royalSlider();
            this.thumbsSwitch();
            this.functionResize();
            this.theSocialActivated();
            this.theSliderActivated();
            this.theSliderDeactivated();
            this.slideEventChange();

            console.log('======> new.global.js');


        },

        // ------------------------------------
        // Functions
        testGlobals: function () {
            console.log(body);
        }

    }

    $(document).ready(function () {
        new global();
    });

})(jQuery);

在我的控制台中,我得到了

Uncaught ReferenceError: body is not defined

我在这里缺少一件简单的事情。

1 个答案:

答案 0 :(得分:0)

尝试替换

testGlobals: function () {
            console.log(body);
        }

testGlobals: function () {
            console.log(this.body);
        }

testGlobals: function () {
            console.log(global.body);
        }
相关问题