Showing posts with label Method. Show all posts
Showing posts with label Method. Show all posts

Wednesday, July 17, 2024

Difference between functions and methods in JavaScript:

 

  1. JavaScript Functions:

    • A function is a block of code designed to perform a specific task.
    • It can be defined using the function keyword, followed by a name and optional parameters.
    • The body of the function is enclosed in curly braces.
    • Functions are executed when something calls or invokes them.
    • Example:
      function addName(a, b) {
          return a + " " + b;
      }
      console.log(addName("Suresh", "Sharma")); // Output: Suresh Sharma
      
  2. JavaScript Methods:

    • A method is a function associated with an object.
    • It is a property of an object that contains a function definition.
    • Methods are functions stored as object properties.
    • They can be accessed using the following syntax: object.methodName().
    • Methods operate on the data contained in a class (i.e., the object it belongs to).
    • Example:
      let employee = {
          empname: "Suresh Sharma",
          department: "Software",
          details: function () {
              return this.empname + " works in "+ this.department +" Department.";    }};
                     console.log(employee.details()); // Output: "Suresh Sharma works in Software Department."

In summary:

  • A function can stand alone and be called directly by its name.
  • A method is a function associated with an object property.
  • Methods implicitly pass the object on which they were called.




AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath