As an absolute beginner, having learned and understood the syntax of a programming language, the next step is to understand how to use the “methods” of the language. I like to think of methods as the vocabulary of a language, the more you understand the easier it is to communicate and write code effectively.
Ok, What are objects?
let person = firstName:"John", lastName:"Doe", fullName: function() return this.firstName + " " + this.lastName; ; // person is the name of the object // firstName, lastName and fullName are the keys // John, Doe, the function are the corresponding values
Now, let’s understand what are methods?
The below lines of code shows an object containing a function. The object name is “objectName” and the method is “methodName”.
const objectName = methodName: function() // Logics to be executed are written ; // methodName is a method of this object - called objectName. // methodName is a function which is a property of the object.
Methods are the block of codes or statements in a program that gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, it provides a better readability of code. So basically, a method is a collection of statements that perform some specific task and returns the result to the caller. A method can also perform some specific task without returning anything.
You can access an object method using a dot notation. The syntax is:
const object = methodName: function() console.log('hello world') // To access the method using dot notation. object.methodName() // hello world
Here, the methodName method is accessed as object.methodName()
If you access a method without the () parentheses, it will return the function definition, to execute a function we invoke the function with ().
The second method is the bracket notation. For the bracket notation, a method is accessed by giving the object name, followed by the property name in quotation and then wrapped in an opening and closing square bracket.
Note that in the code below, the quotations are absence, this is because the object properties has been assigned to a variable.
let propertyName = 'property'; objectName[propertyName]
Features of Methods:
- The method can also be called without using parenthesis.
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.
// Function keyword function functionName() // Code to be executed
// Arrow function const functionName = () => // Code to be executed
Features of Functions:
- 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, it will stop executing and the value is returned to the caller.
Built-in methods are predefined pieces of code in a program or programming framework or a programming language that performs some specific task. This makes programming easy as programmers don’t have to create a new method or function and can simply directly use the built-in methods in their application.
String Methods: String methods help you to work with strings.
Object Methods: Object methods help you to work with strings.
Date Methods: Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of date objects, using either local time or UTC (universal, or GMT) time.
Number Methods:The Number object contains only the default methods that are part of every object’s definition.
Math Method: Math is a built-in object that has properties and methods for mathematical constants and functions.
Window Object Method: The window object represents an open window in a browser.