Search code examples
javascriptfunctionconventions

what is the difference between .function() and a function(args) in javascript


I have a question i have always written functions in javascript for returning or setting values of other elements like so:

function test(x, y){
   return x*y;
}

and call the function like so:

test(20, 50);

but in libraries like jquery you also see functions like so:

var test = something.test();

so my question is what is the difference between the following functions .test() or test() or are they the same and how is this other way of writing a function called?

and how do you write a .function()

Hope to learn something new, sorry if this is a bit random but i am just very curious.


Solution

  • This is a function. It is called as function()

    function test(a, b) {
      return a * b;
    }
    
    console.log(test(2,3));

    This is a method of an object. It is a function that is declared in the object and is called as object.function()

    var myObject = {
      test: function (a, b) {
        return a * b;
      }
    }
    
    console.log(myObject.test(2,3));