简体   繁体   English

为什么我不能更新更新我的JavaScript对象属性值?

[英]Why I can't update update my JavaScript object properties value?

I'm having problem with updating my App object properties value! 我在更新App对象属性值时遇到问题! I'm trying to update the 'visible' property from false to true whenever I'm calling methods within my App object! 每当我在App对象中调用方法时,我都试图将“ visible”属性从false更新为true!

Now I can't figure out why I can't update the values!!! 现在我不知道为什么我不能更新值!!! Could somebody please help me? 有人可以帮我吗?

Here is the code; 这是代码;

var app = app || {};

app.layout = {

    // SIDEBAR
    'sidebar': function(a) {
        var options = {
            'settings': {
                'object': $('.sidebar-left'),
                'visible': false,
                'type': 'wide'
            },
            'open': function() {
                if(options.isActive() === false) {
                    options.settings.visible = true;
                    options.settings.object.addClass('active');
                }
            },
            'close': function() {
                if(options.isActive() === true) {
                    options.settings.visible = false;
                    options.settings.object.removeClass('active');

                }
            },
            'isActive': function() {
                if(options.settings.visible === true) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return options[a];
    }

    // HEADER
    // CONTENT
    // FOOTER
}

The idea behind this little App API is that I don't need to go and manually check whether a container is visible and change the UI manually, I could just call the app.layout methods and that should do the job... 这个小的App API背后的想法是,我不需要手动检查容器是否可见并手动更改UI,我可以调用app.layout方法,这应该可以完成工作...

As you can see, I'm trying to update the 'visible' property value when the two methods of app.layout.sidebar('open')(); 如您所见,当app.layout.sidebar('open')();的两个方法app.layout.sidebar('open')();时,我正在尝试更新'visible'属性值app.layout.sidebar('open')(); and app.layout.sidebar('close')(); app.layout.sidebar('close')(); are called! 叫做!

Thank you 谢谢


Here is the updated version: 这是更新的版本:

http://jsfiddle.net/Farzad/ndb1490v/3/ http://jsfiddle.net/Farzad/ndb1490v/3/

Hope it helps those that are looking to integrate this to their app, as it makes it really easy to keep track of your UI without needing you to manually check every time... 希望它对那些希望将其集成到其应用程序中的用户有所帮助,因为它使跟踪UI变得非常容易,而无需您每次手动进行检查...

However if anybody knows any better approach then make an update to the jsFiddle version and link back in the comments section :) 但是,如果有人知道更好的方法,请对jsFiddle版本进行更新,并在注释部分中链接回来:)

Thank you guys, specially those who being helpful Cheers 谢谢你们,特别是那些有帮助的人

Your code is recreating the options object every time sidebar is called, with false as the hardcoded value of visible : 每次sidebar被调用时,您的代码都会重新创建 options对象,其中falsevisible的硬编码值:

app.layout = {

    // SIDEBAR
    'sidebar': function(a) {
        var options = {
            'settings': {
                'object': $('.sidebar-left'),
                'visible': false,                 // <=== Note
                'type': 'wide'
            },
            // ...
        return options[a];
    }
}

So any call you make like this: 因此, 任何叫你做这样的:

app.layout.sidebar('open')();

...will always see options.settings.visible as false . ...将始终看到options.settings.visiblefalse

I can't see any reason for the complexity in your layout object, and so can't really recommend an appropriate change, other than to make sure options is not recreated each time. 除了确保不会每次都重新创建options ,我看不到layout对象复杂性的任何原因,因此不能真正建议适当的更改。

If you're happy to use this kind of call instead: 如果您很乐意改用这种电话:

app.layout.sidebar.open();

...then you can make things much less complicated: ...然后您可以简化事情:

var app = app || {};

app.layout = (function() {
    var options = {
        'settings': {
            'object': $('.sidebar-left'),
            'visible': false,
            'type': 'wide'
        }
    };
    return {
        sidebar: {
            'open': function() {
                if(options.isActive() === false) {
                    options.settings.visible = true;
                    options.settings.object.addClass('active');
                }
            },
            'close': function() {
                if(options.isActive() === true) {
                    options.settings.visible = false;
                    options.settings.object.removeClass('active');

                }
            },
            'isActive': function() {
                if(options.settings.visible === true) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        // HEADER, e.g.
        ,
        header: {
            // ...
        }
        // CONTENT
        // FOOTER
    };
})();

Note that you need to make sure that code runs after the .sidebar-left element exists. 请注意,您需要确保代码 .sidebar-left元素存在之后运行。


Side note: The quotes you've used in the options object property keys are all optional, you could just leave them off: 旁注:您在options对象属性键中使用的引号都是可选的,您可以将其省略:

var options = {
    settings: {
        object: $('.sidebar-left'),
        visible: false,
        type: 'wide'
    }
};

Same goes for the ones defining sidebar . 定义sidebar The only time you need quotes around property names in JavaScript is if the name is not a invalid IdentifierName (for instance, has spaces in it, or starts with a digit but isn't a number). 在JavaScript中,唯一需要用引号引起来的是名称是否不是无效的IdentifierName (例如,其中包含空格,或者以数字开头但不是数字)。

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

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