Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  131] [ 3]  / answers: 1 / hits: 23211  / 13 Years ago, wed, june 8, 2011, 12:00:00

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();

More From » jquery

 Answers
31

  1. You don't need the fn part, simply use:



    $.DataBar = function () { ... };


    $.fn is simply a reference to jQuery's internal prototype. So $.fn.DataBar is intended to be used as $(selector).DataBar(), not $.DataBar().


  2. Your default options aren't being applied to the newly created object. Optionally, you can also define the greet function on DataBar's prototype:



    $.DataBar = function () {
    $.extend(this, $.DataBar.defaultOptions);
    };

    $.DataBar.prototype.greet = function () {
    alert(this.text);
    };

    $.DataBar.defaultOptions = {
    class: 'DataBar',
    text: 'Enter Text Here'
    };


[#91813] Tuesday, June 7, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
allans

Total Points: 336
Total Questions: 120
Total Answers: 119

Location: Peru
Member since Mon, Jun 6, 2022
2 Years ago
;