简体   繁体   English

在jquery中创建名称空间时出错

[英]Error to create namespace in jquery

I had write a code in js file 我已经在js文件中编写了代码

(function ($) {
    var $r = $.loadaccess;

    jQuery.loadaccess.page.user = {
        init: function () {
            debugger;
            var k = 'dd';
            alert(k);
        }
    };
})(jQuery);

var user = jQuery.loadaccess.page.user;

and I am calling it on .aspx page 我在.aspx页面上调用它

$(document).ready(function () {

        jQuery.loadaccess.page.user.init();
    });

but it thrown error for me "Microsoft JScript runtime error: Unable to get value of the property 'page': object is null or undefined" 但是它为我抛出了错误“ Microsoft JScript运行时错误:无法获取属性'page'的值:对象为null或未定义”

on this 在这个

jQuery.loadaccess.page.user = {
            init: function () {
                debugger;
                var k = 'dd';
                alert(k);
            }
        };

The error is pretty self descriptive, jQuery.loadaccess is null or undefined. 该错误是非常自我描述的, jQuery.loadaccess为null或未定义。 What do you expect jQuery.loadaccess to be? 您期望jQuery.loadaccess是什么? You will need to create this 'namespace' yourself. 您将需要自己创建此“命名空间”。 For example: 例如:

jQuery.loadaccess = {
  page: {
    user: {
      init: function () {
          debugger;
          var k = 'dd';
          alert(k);
      }
    }
  }
};

I wouldn't suggest adding objects to the jQuery object though, rather create your own top level namespace. 我不建议将对象添加到jQuery对象,而是创建自己的顶级名称空间。

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

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