陷入简单的对象文字范围/引用问题

发布于 2024-11-18 06:48:29 字数 889 浏览 3 评论 0原文

这是我的结构示例:

this.is.a.really.long.namespace = {

    inputs : {},
    buttons : {},
    panels : {},

    fn : {

        abc : function() {},
        def : function() {}

    } 

};

现在,如您所见,我将输入、按钮、面板和函数存储在各自的对象文本中。问题出在 fn.abcfn.defpage.fn 内的任何其他函数中。我希望能够从 fn 内部的函数访问我的输入按钮面板

显然,我知道我可以输入 this.is.a.really.long.namespace.inputs,但正如您所看到的,这相当长,而且我不想将其输入对于我需要引用页面内的对象的每个实例。

有没有办法可以从 fn 中直接引用 inputsbuttonspanels

我在想我可以这样做:

fn : {

    that : this.is.a.really.long.namespace,

    abc : function() {},
    def : function() {}

}

这将允许我在 fn.abc 内部使用 that.inputs ,但是该方法有问题吗?我需要注意任何类型的开销吗?或者有更好的方法来实现这一点吗?

Here's a sample of my structure:

this.is.a.really.long.namespace = {

    inputs : {},
    buttons : {},
    panels : {},

    fn : {

        abc : function() {},
        def : function() {}

    } 

};

Now, as you can see I'm storing my inputs, buttons, panels, and functions in their own respective object literals. The issue lies within fn.abc, fn.def, or any other function inside of page.fn. I want to be able to access my inputs, buttons, and panels from within the functions inside of fn.

Obviously, I know I can type this.is.a.really.long.namespace.inputs, but as you can see, that's pretty long, and I don't want to have to type it out for every single instance where I need to reference an object within the page.

Is there not a way I can directly reference inputs, buttons, and panels from within fn?

I was thinking I could do:

fn : {

    that : this.is.a.really.long.namespace,

    abc : function() {},
    def : function() {}

}

which would allow me to use that.inputs inside of fn.abc, but is there a concern with that method? Any sort of overhead I need to be aware of? Or is there a better way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遮了一弯 2024-11-25 06:48:30

这并没有什么问题。事实上,您可能会由于以下原因而减少开销:

  1. 对象层次结构的运行时解析更少
  2. 字符更少 = 脚本更短

更充实的构造是常用的“模块模式”。

Javascript 内部效率相当低(例如,它没有真正的索引数组),因此您可以采取任何措施来减少运行时查找通常都是好的。创建一个指向长层次结构的指针比每次都使用完整的层次结构要快得多。这可能只在长循环中很重要,但由于它也更容易阅读,所以它只是更快一点的好处 - 没有缺点。

(编辑)

要使用直接对象执行此操作,您可以执行类似的操作,使用 jQuery
简化添加属性:

this.is.a.long.namespace = {};
$.extend(this.is.a.long.namespace, 
   { that: this.is.a.long.namespace,
   ... // other properties 
   });

一般来说,如果您正在构建功能对象,则模块模式更好,因为它更灵活,并且允许您使用范围来创建私有变量/对象。

this.is.a.long.namespace = (function() 
{
    var that = {},
       somePrivateVariable;
    function privateFunction() {
      ...
    } 
    that.inputs = {};
    ...
    // assign everything to "that"
    return that;
}());

There is nothing wrong with this. In fact you are likely to reduce overhead for the following reasons:

  1. Less run-time resolution of object heirarchies
  2. Fewer characters = shorter script

A more fleshed out construct is the commonly used "module pattern."

Javascript is fairly inefficient internally (e.g. it doesn't have real indexed arrays) so anything you can do to reduce run-time lookups is usually good. Creating a pointer to a long heirarchy will be much faster than using the full heirarchy every time. This will probably only matter much in long loops, but since it's also easier to read, it's just a bonus of being a bit faster - there is no downside.

(edit)

to do this with straight objects you could do something like this, using jQuery to
simplify adding properties:

this.is.a.long.namespace = {};
$.extend(this.is.a.long.namespace, 
   { that: this.is.a.long.namespace,
   ... // other properties 
   });

Generally speaking though if you're building functional objects the module pattern is better, because it's more flexible and lets you use scope to create private variable/objects.

this.is.a.long.namespace = (function() 
{
    var that = {},
       somePrivateVariable;
    function privateFunction() {
      ...
    } 
    that.inputs = {};
    ...
    // assign everything to "that"
    return that;
}());
神爱温柔 2024-11-25 06:48:30

如果您以每个级别都包含一个指向其上方级别的 parent 参数的方式构建对象,那么您可以通过这种方式访问​​数据。看看这个类似的问题

If you build the object in such a way that every level contains a parent parameter pointing to the level above it, then you can access the data that way. Take a look at this similar question.

虚拟世界 2024-11-25 06:48:30

好吧...这就是我最终所做的:

this.is.a.really.long.namespace = {

    inputs  : { firstName : undefined },
    buttons : { submit : undefined },

    fn : {

        root : undefined,

        abc : function() { console.log(this.root.inputs.firstName); },
        def : function() { console.log(this.root.buttons.submit); }

    },

    init : function() {

        var self     = this,
            fn       = self.fn,
            inputs   = self.inputs,
            buttons  = self.button;

            fn.root  = this;  // this is the key

            inputs.firstName  = $("#first-name");
            buttons.submit    = $("#submit-button");

        fn.abc();
        fn.def();

    }

};

Ok ... Here's what I ended up doing:

this.is.a.really.long.namespace = {

    inputs  : { firstName : undefined },
    buttons : { submit : undefined },

    fn : {

        root : undefined,

        abc : function() { console.log(this.root.inputs.firstName); },
        def : function() { console.log(this.root.buttons.submit); }

    },

    init : function() {

        var self     = this,
            fn       = self.fn,
            inputs   = self.inputs,
            buttons  = self.button;

            fn.root  = this;  // this is the key

            inputs.firstName  = $("#first-name");
            buttons.submit    = $("#submit-button");

        fn.abc();
        fn.def();

    }

};
方圜几里 2024-11-25 06:48:30

我会考虑这样的事情:

var namespace = this.is.a.really.long.namespace
this.is.a.really.long.namespace = {

    root : namespace,
    inputs : {},
    buttons : {},
    panels : {},

    fn : {

        abc : function() {},
        def : function() {}

    } 
};

从那里您在对象内的任何位置引用您的名称空间应该没有问题。

I would consider something like this:

var namespace = this.is.a.really.long.namespace
this.is.a.really.long.namespace = {

    root : namespace,
    inputs : {},
    buttons : {},
    panels : {},

    fn : {

        abc : function() {},
        def : function() {}

    } 
};

from there you should have no problems referencing your namespace anywhere within the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文