我创建了一个JS对象字面量,但是想在不在函数中的情况下访问属性。每当我尝试时,我没有得到任何结果,或者它返回null
。
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??
}
}
在上面的代码中,我试图使用
this.slides
访问slides
,都在同一个对象内部,但控制台显示为null
。但是当我执行方法inDent:$('#slides').find('li').outerWidth()
时,它就可以正常工作了。为什么会这样?我认为它与在函数内使用this
关键字使用这些属性相同,显然不是这种情况。我只想将该对象属性的值即字符串传递给另一个不是函数的属性。非常感谢