Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialchazel
Courses Plus Student 6,961 PointsPassing an Object Function - Uncaught TypeError
Summary
I am trying to pass an object function (either _.convertGold or _.convertSilver in the below example) as an additional function to perform inside a callback function. Normally, inside the swap() method shown below, I could use _.convertGold() or _.convertSilver() to execute those functions.
swap: function () {
var _ = this;
var browserWidth = $(window).width();
function setConversionEvent(state) {
_.$appEl.trigger($.Event('myModule.App.converted'));
_.convertedState = state;
}
function convert(callback, fn, state) {
fn();
callback(state);
}
if (browserWidth <= 768) {
if (_.convertedState === 'silver') {
convert(setConversionEvent, _.convertGold, 'gold')
}
} else {
if (_.convertedState === 'gold') {
convert(setConversionEvent, _.convertSilver, 'silver')
}
}
},
Problem
When I try passing those functions, I see the following errors:
Firefox: Uncaught TypeError: _ is undefined
Chrome: Uncaught TypeError: Cannot read property '$appEl' of undefined
I am not sure how to resolve "this" problem here. Can anyone help? I have copied below a generic shell of the module pattern I use so you can see the bigger picture of the code structure:
(function ($) {
'use strict';
jQuery.fn.extend({
myModule: function () {
var App = function (element, options) {
this.element = element;
this.options = options;
this.metadata = $(element).data('app-options');
};
App.prototype = {
events: {
init: 'myModule.App.init'
},
setOptions: function () {
var _ = this;
// merge defaults and other options
_.options = $.extend({}, _.defaults, _.options, _.metadata);
_.options.error = '';
// cache element for best performance
_.$appEl = $(_.element);
},
convertSilver: function () {
var _ = this;
var el = _.$appEl;
...
_.convertedState = 'silver';
},
convertGold: function () {
var _ = this;
var el = _.$appEl;
...
_.convertedState = 'gold';
},
swap: function () {
var _ = this;
var browserWidth = $(window).width();
function setConversionEvent(state) {
_.$appEl.trigger($.Event('myModule.App.converted'));
_.convertedState = state;
}
function convert(callback, fn, state) {
fn();
callback(state);
}
if (browserWidth <= 768) {
if (_.convertedState === 'silver') {
convert(setConversionEvent, _.convertGold, 'gold')
}
} else {
if (_.convertedState === 'gold') {
convert(setConversionEvent, _.convertSilver, 'silver')
}
}
},
watchBrowser: function () {
var _ = this;
$(window).on('load resize', function () {
_.swap();
});
},
setActions: function () {
var _ = this;
...
},
init: function () {
var _ = this;
_.setOptions();
_.$appEl.trigger($.Event(_.events.init));
_.convertedState = 'silver'; // set initial state before conversion
_.swap();
_.watchBrowser();
_.setActions();
}
};
App.defaults = App.prototype.defaults;
$.fn.app = function (options) {
return this.each(function (i, element) {
new App(element, options).init();
});
};
window.App = App;
$(window).on('load', function () {
$('[data-app-options]').app();
});
}
});
jQuery(document).myModule();