简体   繁体   English

在Javascript中访问对象文字

[英]Accessing object literals in Javascript

This code is an example from Marionette: 此代码是Marionette的示例:

AppLayout = Backbone.Marionette.Layout.extend(
{
    template: "#layout-template",

    regions: 
    {
        menu: "#menu",
        content: "#content"
    }
});

var layout = new AppLayout();

layout.menu.show(new MenuView());
layout.content.show(new MainContentView());

The last two lines confuse me. 最后两行使我感到困惑。 Why doesn't it read: 为什么不显示:

layout.regions.menu.show(new MenuView());
layout.regions.content.show(new MainContentView());

Can someone please explain why layout.menu works and layout.regions.menu doesn't? 有人可以解释为什么layout.menu起作用而layout.regions.menu不能起作用吗?

What if I wanted to access template? 如果我想访问模板怎么办? Wouldn't that be layout.template? 那不是layout.template吗? template and regions are at the same depth inside layout. 模板和区域在布局内的深度相同。

Here is the constructor function from the marionette code: 这是牵线木偶代码的构造函数:

// Ensure the regions are avialable when the `initialize` method
        // is called.
        constructor: function () {
            this._firstRender = true;
            this.initializeRegions();

            var args = Array.prototype.slice.apply(arguments);
            Marionette.ItemView.apply(this, args);
        },

I believe it was implemented that way because 'layout.menu' is shorter and simpler than 'layout.regions.menu'. 我相信这样做是因为“ layout.menu”比“ layout.regions.menu”更短,更简单。 Looks like you expected the literal "#menu" to be replaced with a region manager object. 看起来您希望将文字“ #menu”替换为区域管理器对象。

The options you passed in when creating the view, including the template, can be found in layout.options. 您在创建视图时传递的选项(包括模板)可以在layout.options中找到。 So in your case layout.options.template should equal '#layout-template', and the regions definition hash would be at layout.options.regions... still the same level. 因此,在您的情况下,layout.options.template应该等于'#layout-template',并且区域定义哈希将位于layout.options.regions ...仍处于同一级别。

Unless there is more to the example then you are showing like the Backbone.Marionette.Layout methods, then its not accessing regions.menu like you think it is. 除非该示例有更多内容,否则您将像Backbone.Marionette.Layout方法那样显示,那么它就不会像您认为的那样访问regions.menu

With just the code you have provided the code above is actually creating a menu attribute, which then has a show attribute so your layout object would actually look like this: 仅使用您提供的代码,上面的代码实际上创建了一个menu属性,然后它具有一个show属性,因此您的layout对象实际上如下所示:

layout {
    menu : {
        show : new MenuView
    },
    content : {
         show : new MainContentView
    },

    template: "#layout-template",

    regions: 
    {
        menu: "#menu",
        content: "#content"
    }
}

In javascript the (dot) operator can be used to access a property of an attribute or if no property with that name exists then it will create that property. 在javascript中, (dot)运算符可用于访问属性的属性,或者如果不存在具有该名称的属性,则它将创建该属性。

I'm not familiar with the backbone.js framework but my guess is that they provide for skipping part of the property lookup chain. 我不熟悉backbone.js框架,但我猜想它们提供了跳过属性查找链的一部分的功能 which means that the above would end up producing this as your layout object: 这意味着以上内容最终将其作为布局对象生成:

 layout {
    template: "#layout-template",

    regions: 
    {
        menu : {
            show : new MenuView
        },
        content : {
            show : new MainContentView
        }
    }
}

But again that's just a guess on my part since I don't use backbone . 但这又只是我的猜测,因为我不使用backbone

You can learn more about the object model and how it works with inheritance right here. 您可以在此处了解有关对象模型及其如何与继承一起工作的更多信息

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

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