I am trying to better understand the object literal and I'm stuck on this specific part.
I have a fully working example here - https://jsfiddle.net/breezy/dqjpkus4/
I am trying to use the settings throughout the object but the console is saying my items are undefined. But, I wanted to convert this into an object. You can see my work in progress here and where the errors are happening - http://codepen.io/BryanBarrera/pen/OXZvgR
My question is how can I access this portion:
boxes: $('.boxes'),
container: $('.container'),
From inside my functions?
showMore: function() {
},
showLess: function() {
},
Another example of what I am trying to do - Here is the fiddle for the below where I am stuck. https://jsfiddle.net/breezy/86Lryrep/
The init
portion works but when I try to execute the function on click, it gives me an error.
"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);