This is a simple question I know, I’ve looked on google but can’t find much help. I’m trying to create an object, with my own custom parameters, and then call one of them in an alert.
Whatever I try, doesn’t seem to work, I know this is pretty simple stuff and I appologise! All my other JS in my time have been pretty simple and all inline because of that, I’m moving on to more OOP JS now.
$.fn.DataBar = function() {
$.DataBar.defaultOptions = {
class: 'DataBar',
text: 'Enter Text Here'
}
this.greet = function() {
alert(this.text);
};
}
var q = new $.DataBar();
q.greet();
You don’t need the
fnpart, simply use:$.fnis simply a reference to jQuery’s internal prototype. So$.fn.DataBaris intended to be used as$(selector).DataBar(), not$.DataBar().Your default options aren’t being applied to the newly created object. Optionally, you can also define the
greetfunction onDataBar‘s prototype: