如何在Javascript中访问函数外部的变量

时间:2010-04-30 19:04:56

标签: javascript jquery

以下是我正在使用的代码:

$(document).ready(function () {

    var TemplateEditor = function () {

        var GroupClassName = 'group';
        var SelectedGroup = 0;

        var BindClicks = function () {
            $('.CriteriaSelections').unbind('click').click(function (event) {
                event.preventDefault();
                if (fnIsTheClickedBoxaGroup($(this))) {
                    TemplateEditor.GroupClicked();
                }
                else {
                    TemplateEditor.CriteriaClicked($(this), SelectedGroup);
                }
            });

            $('.groupselected').unbind('click').click(function (event) {
                event.preventDefault();
                SelectedGroup = $(this).attr('group-'.length);
                TemplateEditor.SelectGroup(SelectedGroup);
            });
        }


        var fnGetGroupID = function (Obj) {
            if (fnIsTheClickedBoxaGroup(Obj) == true) {
                return null;
            }
            else {
                //Get parent which is the group
                var ObjParent = Obj.parent();
                var GID = ObjParent.attr('id').substr('group-'.length);
                return GID;
            }
        }

        var fnIsTheClickedBoxaGroup = function (Obj) {
            var GetClass = Obj.attr('class');
            if (GetClass == GroupClassName) {
                return true;
            }
            else {
                return false;
            }
        }

        return {
            Init: function () {
                BindClicks();
            },
            CriteriaClicked: function (Obj, GroupID) {
                $('<div>').attr({ id: '' }).addClass('selection').text(Obj).appendTo('#group-' + GroupID);
            },
            GroupClicked: function () {

            },
            SelectGroupClicked: function () {

            },
            UpdateTargetPanel: function () {

            }
        };
    } ();
    TemplateEditor.Init();
});

我正在尝试访问此变量:      GroupClassName

此变量位于此函数

var fnIsTheClickedBoxaGroup = function (Obj) {
                var GetClass = Obj.attr('class');
                if (GetClass == GroupClassName) {
                    return true;
                }
                else {
                    return false;
                }
            }

当我运行该程序时,它表示GroupClassName未定义。我在这里错过了什么吗?

5 个答案:

答案 0 :(得分:3)

试试这个

function fnIsTheClickedBoxaGroup(Obj) {
  var GetClass = Obj.attr('class');
  return (GetClass == GroupClassName);
}

答案 1 :(得分:3)

您发布的代码正常运行。试一试here并查看。

我为测试做出的唯一更改是在致电fnIsTheClickedBoxaGroup之后:

if (fnIsTheClickedBoxaGroup($(this))) {
    alert('fnIsTheClickedBoxaGroup returned true');
    //TemplateEditor.GroupClicked();
}
else {
    alert('fnIsTheClickedBoxaGroup returned false');
    //TemplateEditor.CriteriaClicked($(this), SelectedGroup);
} 

由于此处未显示的内容,您收到错误消息。也许您在其他位置使用GroupClassName

答案 2 :(得分:2)

或者,因为该变量是'常量',所以只需在TemplateEditor范围之外创建它,就在ready事件中。

答案 3 :(得分:2)

$(document).ready(function () {

    var TemplateEditor = (function () {

        var __self = this;

        __self.groupClassName = 'group',
        __self.selectedGroup = 0;

        __self.BindClicks = function () {

            $('.CriteriaSelections').unbind('click').click(function (event) {
                event.preventDefault();
                if (__self.fnIsTheClickedBoxaGroup($(this))) {
                    TemplateEditor.GroupClicked();
                }
                else {
                    TemplateEditor.CriteriaClicked($(this), __self.selectedGroup);
                }
            });

            $('.groupselected').unbind('click').click(function (event) {
                event.preventDefault();
                __self.selectedGroup = $(this).attr('group-'.length);
                TemplateEditor.SelectGroup(__self.selectedGroup);
            });
        }


        __self.fnGetGroupID = function (Obj) {
            if (__self.fnIsTheClickedBoxaGroup(Obj) == true) {
                return null;
            }
            else {
                //Get parent which is the group
                var ObjParent = Obj.parent();
                var GID = ObjParent.attr('id').substr('group-'.length);
                return GID;
            }
        }

        __self.fnIsTheClickedBoxaGroup = function (Obj) {
            var GetClass = Obj.attr('class');
            if (GetClass == __self.groupClassName) {
                return true;
            }
            else {
                return false;
            }
        }

        return {
            Init: function () {
                __self.BindClicks();
            },
            CriteriaClicked: function (Obj, GroupID) {
                $('<div>').attr({ id: '' }).addClass('selection').text(Obj);
                // text return text, don't jQuery object
                $('<div>').appendTo('#group-' + GroupID);
            },
            GroupClicked: function () {

            },
            SelectGroupClicked: function () {

            },
            UpdateTargetPanel: function () {

            }
        };

    }).call({});

    TemplateEditor.Init();
});

答案 4 :(得分:-1)

如果将其更改为this.GroupClassName,是否有效?