Objects

Methods in JavaScript

Methods are functions declared within objects.


Syntax
var objectName = {
    methodName: function(passedParams) {
        //code to execute on method call
    }
}

objectName.methodName(passedParams);

Notes

A function must be called in the context of an object.


Example
var Calculator = {
    add: function(a, b) {
        return a + b;
    }
    subtract: function(a, b) {
        return a - b;
    }
}

console.log(Calculator.add(1, 1));

< Object Declaration   |   Create Object using Constructor >

© 2019 SyntaxDB. All Rights Reserved.