Jelaskan apa yang dimaksud dengan function atau method pada javascript?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    JavaScript Functions: A function is a block of code written to perform some specific set of tasks. We can define a function using the function keyword, followed by Name and optional parameters. Body of function is enclosed in Curly braces.

    Syntax: 

    function functionName[parameters] {
        // Content
    }

    Features:

    • The function is executed when something calls/invokes it.
    • The name may contain letters, digits, dollar signs, underscore.
    • Parameters are listed inside round parenthesis after the name of the function.
    • Arguments are values a function receives when it is invoked.
    • When the control reaches the return statement, js will stop executing and the value is returned to the caller.

    Example: Below is the function to add two numbers.

    Javascript

    function func[a, b] {

    var sum = a + b;

    return sum;

    }

    console.log[func[1, 2]];

    Output:

    3

    JavaScript Methods: A JavaScript method is a property of an object that contains a function definition. Methods are functions stored as object properties. Object method can be accessed with the following syntax:

    Syntax:

    object = {
        methodName: function[] {
            // Content
        }
    };
    
    object.methodName[]

    Features:

    • Actions that can be performed on objects are what we term JavaScript methods.
    • The objects can also be called without using parenthesis.
    • This refers to the owner object in a method.

    Example: The following example shows the method that is invoked with an object called employee.

    Javascript

        

            

                var employee = {

                    empname: "Rahul",

                    department : "sales",

                    details : function[] {

                        return this.empname +

                            " works with Department " +

                            this.department;

                    }

                };

                console.log[employee.details[]];

            

        

    Output:

    Rahul works with Department sales

    Difference Between Function and Method:

    Function

    Method

    A JavaScript function is a block of code designed to perform a particular task. The javascript method is an object property that has a function value.

    Syntax of Function is -:

    function functionName[parameters] {
       // Content
    }

    Syntax of Method is -:

    object = {
       methodName: function[] {
           // Content
       }
    };

    object.methodName[]

    A function can pass the data that is operated and may return the data.  The method operates the data contained in a Class.
    Data passed to a function is explicit. A method implicitly passes the object on which it was called.
    A function lives on its own. A method is a function associated with an object property.
    A function can be called directly by its name  A method consists of a code that can be called by the name of its object and its method name using dot notation or square bracket notation.
    Functions are helpful because it increases the reusability of the code. Javascript includes some in-built methods also for example -: parseInt[] Method
    The [] Operator is used to Invoke the Function

    We can access object method by following syntax -:

    objectName.methodName[]


    Apa yang dimaksud dengan function dan method?

    Secara singkat perbedaan antara fungsi dan metode adalah fungsi adalah serangkaian instruksi untuk menjalankan sebuah tugas, sedangkan metode adalah serangkaian instruksi yang berkaitan dengan sebuah object.

    Apa yang dimaksud dengan function dalam JavaScript?

    Fungsi pada JavaScript adalah sekumpulan kode yang dirancang untuk melakukan tugas tertentu. Sebuah fungsi JavaScript dijalankan ketika ada yang memanggilnya.

    Apa yang dimaksud dengan function?

    Fungsi [function] adalah sub modul atau sub program yang dibuat untuk menyelesaikan hal tertentu. Fungsi memiliki nama dan dalam ANSI/C, diimplementasi untuk hal-hal yang khusus dan dapat dipanggil berkali-kali [berulang] dalam program.

    Apa itu function pada JavaScript dan kenapa harus ada?

    Fungsi atau function di javascript adalah sebuah blok kode yang digunakan untuk membungkus suatu proses dengan tujuan agar penulisan kode atau proses yang sama tidak ditulis secara berulang kali.

    Bài mới nhất

    Chủ Đề