简体   繁体   English

如何在不使用javascript函数的情况下使用对象文字的属性

[英]How to use properties of an object literal without being inside a function in javascript

I created an object literal in JS, but wanna to access a property without being in a function. 我在JS中创建了一个对象文字,但是想要在不使用函数的情况下访问属性。 Whenever I try, I don't get any results or it returns null . 每当我尝试,我没有得到任何结果或它返回null

JS: JS:

var settings = {
        prev: '#prev',
        next: '#next',
        slides: '#slides',
        crslContainer: '#carousel',

        //I'm trying to access the 'slides' by using 'this.slides'
        inDent: $(this.slides).find('li').outerWidth(),
        fx: 'rotate',

        myFunction: function () {
          console.log(settings.inDent); //This shows null in the console, why??
        }
    }

In the code above I'm trying to access slides by using this.slides , all inside the same object, but the console says null . 在上面的代码中,我试图通过使用this.slides访问slides ,所有这些都在同一个对象中,但控制台显示为null But when I do the method, inDent: $('#slides').find('li').outerWidth() , then it works. 但是当我使用方法时, inDent: $('#slides').find('li').outerWidth() ,然后它就可以了。 Why is that? 这是为什么? I assumed it's the same as using those properties inside a function by using this keyword, apparently it's not the case. 我假设它与使用this关键字在函数内使用这些属性相同,显然情况并非如此。 I just wanna pass the value of that object property , ie a string, to another property that's not a function. 我只想将该对象属性(即字符串)的值传递给另一个不是函数的属性。

Many thanks 非常感谢

You can't refer to the "object being defined" in an object literal. 您不能在对象文字中引用“正在定义的对象”。 That is, there's no way to have the value expression for a property of the under-construction object refer to the object itself in order to access another (presumably, already-defined) property. 也就是说,没有办法让under-construction对象的属性的值表达式引用对象本身,以便访问另一个(可能是已经定义的)属性。

You can however add a property to the object after it's been defined. 但是,您可以在定义对象后向其添加属性。

 var obj = { a: 0 };
 obj.b = (obj.a ? 1 : 2);

Or in your case: 或者在你的情况下:

 var settings = { ... };
 settings.inDent = ($(settings.slides).find('li').outerWidth());

You need to use 你需要使用

this.inDent

The inDent field would be accessed via settings.inDent only outside of the settings object definition 只能在settings对象定义之外通过settings.inDent访问inDent字段

Use closures, eg 使用闭包,例如

var settings = function(){
    var private = {};
    private.slides = '#slides';

    var public = {}
    public.inDent = ($(private.slides).find('li').outerWidth());
    public.doSomething = function(){
        console.log( public.inDent );
    }

    return public;
}();

The advantage of this is also that it gives you "encapsulation" for free 这样做的好处还在于它可以免费为您提供“封装”

BTW, you shouldn't rely on anything that is publicly available, because, well, it can be changed, eg settings.inDent = null and then settings.doSomething() may no longer function correctly. 顺便说一下,你不应该依赖任何公开的东西,因为它可以改变,例如settings.inDent = null然后settings.doSomething()可能不再正常运行。 What correct way to do it is the following 正确的做法是以下几点

...
private.inDent = ($(settings.slides).find('li').outerWidth());

public.inDent = private.inDent;
public.doSomething = function(){
    console.log( private.inDent );
}

ie make the inDent value "read only" (in a sense that nothing outside of the settings object can actually change the internal implementation of private.inDent ); 即使inDent值“只读”(在某种意义上说, settings对象之外的任何内容都不能实际更改private.inDent的内部实现); as long as you always use private.inDent from within settings you're safe because even if someone does settings.inDent = null , the settings.doSomething(); 只要你总是在settings使用private.inDent你是安全的,因为即使有人做settings.inDent = nullsettings.doSomething(); will still function properly 仍将正常运作

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

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