26

I mean object as in {} [object Object]. How does it do $(selector) and $.fn.init at the same time?

Can you give me a simple example please?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
David G
  • 94,763
  • 41
  • 167
  • 253
  • So how is it work as an object as well? – David G Aug 28 '11 at 20:12
  • @David Because in JavaScript functions are objects `:)`. You can view functions as advanced objects - they can do everything that regular objects can do, but they also have a few additional features... – Šime Vidas Aug 28 '11 at 20:16
  • possible duplicate of [How is the "jQuery" var a function and an object?](http://stackoverflow.com/questions/2724784/how-is-the-jquery-var-a-function-and-an-object) – user113716 Aug 28 '11 at 20:18
  • that's the whole point of functional programming. you can pass functions as arguments, return them as arguments, etc.. they has to be objects. – Karoly Horvath Aug 28 '11 at 20:19

8 Answers8

35

This isn't unique to jQuery, but an aspect of javascript. All functions are objects. E.g.:

var f = function() { alert('yo'); }
f.foo = "bar";

alert(f.foo); // alerts "bar"
f();          // alerts "yo"
numbers1311407
  • 33,686
  • 9
  • 90
  • 92
6

Javascript is an object oriented language, so functions ARE objects, just fancy ones that you can call.

foo = function() { console.log("foo") }
foo.bar = function() { console.log("bar") }
foo() //=> prints "foo"
foo.bar() //=> prints "bar"
Matt Briggs
  • 41,224
  • 16
  • 95
  • 126
  • 3
    That should probably be "an object-oriented _functional_ language", since being object oriented alone does not imply that functions are first class. – Mike Caron Aug 28 '11 at 20:30
6

$ is a function. a method of $ can return any thing.

For example:

$ = function() {
     return {
                 foo : function() { return 'baa'; },
                 r1: 1,
                 r2 : 'string'
            }
};

typeof $ <- function 
typeof $() <- object
typeof $().foo <- function 
typeof $().foo() <- string
typeof $().r1; <- number 
typeof $().r2 <- string
Kakashi
  • 2,165
  • 14
  • 19
  • This should be the answer, well I think it needs a little bit of explanation but after a while of playing with it I understood, Thanks you have helped me a lot! – Heriberto Juarez Jan 29 '18 at 03:02
3

jQuery or $ is an function(you know $ is an alias of jQuery).

// Define a local copy of jQuery
jQuery = function( selector, context ) {
    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init( selector, context, rootjQuery );
},

In Javascript, everything is an object even function too. Hence You can directly add properties to function.

   jQuery.find = function () {
   }
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
2
var q=function(){};

var s = function(){
    alert("base");
    window.s = s;
    return new q()};

q.fn = q.prototype = {};

q.fn.x = s.x = function(){alert("x");return this;};
q.fn.y = s.y = function(){alert("y");return this;};
q.fn.z = s.z = function(){alert("z");return this;};

s().y().z().x();
s.z().x().y();
Lumic
  • 53
  • 2
  • 3
    Code only answers are usually [frowned upon](http://meta.stackoverflow.com/q/258673/2596334). Try adding an [explination or code comments](http://meta.stackoverflow.com/q/269981/2596334). Thanks. – Scott Solmer Nov 07 '14 at 22:04
2

It is an object.

$ holds different functions.

You can test this yourself by making your own object:

var $ = {
     select: function(id){return document.getElementById(id);}
}

function $(id){
      return $.select(id);
}
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • This doesn't work for me. It returns "object is not a function" when I try it out -- http://jsfiddle.net/Ew2Dx/ – David G Aug 30 '11 at 15:34
2

people play around with javascript functions and it leads to interesting design patterns.. Jquery uses of many of these patterns and creates a nice wrapper around many functions.. so ultimately jquery is like a static class using which one can do really neat stuff..

like all classes it has a name and the default Name is jQuery. The $ is nothing buy an identifier that is tied into the jQuery Library and stops you from having to type “jQuery” as the identifier.

The fact that it is a $ symbol is arbitrary. At some point a decision was made to use the $ symbol but the fact of the matter is that it could have been almost any type of ECMAScript acceptable identifier.

The main reason we use $ as the identifier is that you are less likely to make simple typo mistakes when typing one character instead of a string.

Hope that makes things clear.. please correct me guys if I hv got something wrong

A simple example of say my own library would be say a calculator class

var Calculator= (function()
{

    function add(a,b)
    {
     return a+b;
    }
    function subtract(a,b)
    {
     return a-b;
    }
    function multiply()
    {
     return a*b;
    }
    function log()
    {
      console.log("log 123")
    }
    return
    {
     add: add,
     subtract: subtract,
     multiply: multiply
    }
}());

Now I can perform operation using the Calculator class as follows:

Calculator.multiply(Calculator.add(2,3),5);

add my local functions are private and are not exposed to be used outside. In this case my log function can't be accessed using Calculator.log it will say method not found on object.

Now coming back to your question you could do something like this:

var _=Calculator;

and now use the calc functions like

_.multiply(_.add(2,3),5);

Interestingly there is a library called underscore too..

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
1
var s = function(){};
s.test = function(){console.log('inside s');}
s.test();

is perfectly legal code.

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89