Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery create object

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();
like image 782
Tom Gullen Avatar asked Jun 08 '11 12:06

Tom Gullen


People also ask

How do you object in jQuery?

Another way to make objects in Javascript using JQuery , getting data from the dom and pass it to the object Box and, for example, store them in an array of Boxes, could be: var box = {}; // my object var boxes = []; // my array $('div. test'). each(function (index, value) { color = $('p', this).

What is object object in jQuery?

Introduction to jQuery object. The jQuery object is a collection of DOM elements and behaves like a special array. Everything in jQuery is an object.

How can you create an object in JavaScript?

Creating a JavaScript ObjectCreate a single object, using an object literal. Create a single object, with the keyword new . Define an object constructor, and then create objects of the constructed type. Create an object using Object.create() .

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


2 Answers

  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'
    };
    
like image 155
David Tang Avatar answered Oct 19 '22 17:10

David Tang


There are 4 3 problems in your code

  1. a missing ; after the default options (not causing the error)
  2. add the default options to the instance with this.defaultOptions
  3. call alert(this.defaultOptions.text)
  4. instantiate with $.fn.DataBar() as you added your class to $.fn

Here your code working:

$.fn.DataBar = function() {

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

        this.greet = function() {
            alert(this.defaultOptions.text);
        };
};

var q = new $.fn.DataBar();
q.greet();
like image 37
DanielB Avatar answered Oct 19 '22 16:10

DanielB