www.webdeveloper.com
Results 1 to 6 of 6

Thread: Difference between methods and functions

  1. #1
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    15

    Post Difference between methods and functions

    HI )I am learning java script these days and i just want you guys to clarify me the difference between methods and functions.I mean do both of them serve the same purpose?
    Thanks)

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    Functions are pieces of code not bound to any object, so you can call them on different objects as you please. Methods are object properties that also are functions. In other words, methods are functions which have been bound to an object already. If you want an external reference for more information, check out this link.

  3. #3
    Join Date
    Apr 2014
    Posts
    8
    As Error404 mentioned, methods and functions are very similar, except for where they reside. If you had an object named Grill, and a method named cookBurgers(), you could tell that object to cook burgers by calling this method. However, you cannot cookBurgers() without a Grill.

    So why do you need functions if you're writing object-oriented code?
    Consider the Grill object again. We want to be able to cook burgers, but we would also like to cook chicken. These are very similar actions, but require fundamentally different instructions. Now consider that in javascript, you can pass a function as a parameter. If we create a method on our Grill object called cook, which takes a function parameter, we can now tell the same object to use either outside function.

    To Recap:
    Using methods to achieve the same result, we would make a Grill object, and create two methods called cookBurgers() and cookChicken().
    Grill Burgers:
    Grill.cookBurgers();
    Grill Chicken:
    Grill.cookChicken();

    Passing functions to our Grill object would allow us to pass in functionality to existing code.
    Grill.cook = function(cookingFunction) {
    cookingFunction();
    alert("Come and get it!");
    }

    we would then call this method with

    Grill.cook(cookBurgers());
    Grill.cook(cookChicken());

    The advantage to this being that we can add new behaviors to our Grill object without editing its code later.

  4. #4
    Join Date
    Aug 2008
    Posts
    40
    Nice analogy. . .

  5. #5
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    15
    Quote Originally Posted by lithium0735 View Post
    As Error404 mentioned, methods and functions are very similar, except for where they reside. If you had an object named Grill, and a method named cookBurgers(), you could tell that object to cook burgers by calling this method. However, you cannot cookBurgers() without a Grill.

    So why do you need functions if you're writing object-oriented code?
    Consider the Grill object again. We want to be able to cook burgers, but we would also like to cook chicken. These are very similar actions, but require fundamentally different instructions. Now consider that in javascript, you can pass a function as a parameter. If we create a method on our Grill object called cook, which takes a function parameter, we can now tell the same object to use either outside function.

    To Recap:
    Using methods to achieve the same result, we would make a Grill object, and create two methods called cookBurgers() and cookChicken().
    Grill Burgers:
    Grill.cookBurgers();
    Grill Chicken:
    Grill.cookChicken();

    Passing functions to our Grill object would allow us to pass in functionality to existing code.
    Grill.cook = function(cookingFunction) {
    cookingFunction();
    alert("Come and get it!");
    }

    we would then call this method with

    Grill.cook(cookBurgers());
    Grill.cook(cookChicken());

    The advantage to this being that we can add new behaviors to our Grill object without editing its code later.
    That was amazing now everything is crystal and clear ) Thanks a lot

  6. #6
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    15
    Quote Originally Posted by Error404 View Post
    Functions are pieces of code not bound to any object, so you can call them on different objects as you please. Methods are object properties that also are functions. In other words, methods are functions which have been bound to an object already. If you want an external reference for more information, check out this link.
    Thanks for the clarification )

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles