我想更好地理解對象字面量,並將其卡在這個特定的部分。如何在對象中執行函數和引用變量
我這裏有一個完全工作的例子 - https://jsfiddle.net/breezy/dqjpkus4/
我想整個對象使用的設置,但控制檯說我的項目是不確定的。但是,我想將其轉換爲對象。你可以在這裏看到我的工作進展,並在錯誤正在發生 - http://codepen.io/BryanBarrera/pen/OXZvgR
我的問題是我怎麼可以訪問此部分:
boxes: $('.boxes'),
container: $('.container'),
從裏面我的功能呢?
showMore: function() {
},
showLess: function() {
},
我想要做的另一個例子 - 下面是我被卡住的小提琴。 https://jsfiddle.net/breezy/86Lryrep/
init
部分工作正常,但是當我嘗試在點擊時執行該功能時,它給了我一個錯誤。的this
"use strict";
var object = {
container: $('.container'),
item: $('div'),
textToAlert: 'Working',
init: function() {
console.log(this.textToAlert);
},
firstFunction: function() {
// here i should be able to use container and item
// but when i use my click event it doesn't work.
console.log(this.item.length);
},
secondFunction: function() {
// here i should be able to use container and item
}
}
object.init();
$('button').on('click', object.firstFunction);
'this'的'firstFunction'內部將是指被點擊該元素。你可以將'this'緩存到_that_或_self_或使用'.bind(object)'。 – Tushar
如何訪問'item:$('div')'而不是按鈕?這部分我很困惑。 @Tushar – breezy