
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to pass an object as a parameter in JavaScript function?
In this tutorial, we will learn how to pass an object as a parameter in a JavaScript function.
One of JavaScript's data types is represented by the Object type. It is used to store keyed collections as well as more complex entities. The Object() function Object() or the object initializer / literal syntax can be used to create objects.
Almost all JavaScript objects are instances of Object; a typical object inherits properties (including methods) from Object.prototype, though these properties may be shadowed (a.k.a. overridden).
Following are the methods used to pass an object as a parameter in a JavaScript function.
Using the Literal Object Notation
This is extremely straightforward. You have to just place your key-value pairs separated by ':' inside a set of curly braces(), and your object will be ready to serve (or consumed).
We can pass an object to a JavaScript function, but the arguments must have the same names as the Object property names.
Syntax
function areaOfRectangle({ l,b }) { return l * b; } let rectangle = { l: 22, b: 10 }; areaOfRectangle(rectangle);
The rectangle object takes the input of length and breadth of a rectangle whose area is calculated when this object is passed as a parameter to the areaofRectangle() function.
Example
The rectangle object is created using the literal notation object in this example. The rectangle’s length and breadth are passed into this object. We create the areaOfRectangle() function to calculate the area of the rectangle. This function is called, and the rectangle object is passed to it.
<html> <body> <p id="result"></p> <script> let output = document.getElementById("result"); // define a function function areaOfRectangle({l, b}) { return (l * b); } // define an object let rectangle = {l: 77, b: 21 }; // Call the function passing the object to fucntion as parameter. let area = areaOfRectangle(rectangle); output.innerHTML = "Area of rectangle : " + area + "<br>"; </script> </body> </html>
Using this and prototype
Compared to other languages, the "this" keyword in a function behaves slightly differently in JavaScript. It also distinguishes between strict and non-strict modes.
This value is usually determined by how a function is called (runtime binding). It cannot be set during execution and may differ each time the function is called. The bind() method can change the value of a function's this no matter how it's called, and arrow functions don't have their binding (it retains this value of the enclosing lexical context).
JavaScript objects inherit features from one another through the use of prototypes. This article will explain a prototype, how prototype chains work, and how to set a prototype for an object.
Syntax
var func = function(param1) { this.param1 = param1; }; func.prototype.display = function() { return this.param1; };
The function func() takes a parameter param, which is passed to the display function to display its value.
Example
In the example, we have created an object in the variable res. This variable calls the function func() with a parameter of 11.98. To make an object, use the new keyword in conjunction with the Object() function Object(), and then add properties to this object. This function passes the parameter as an object and helps to display it using the display() function.
<html> <body> <script> var func = function(param1) { this.param1 = param1; }; func.prototype.display = function() { return this.param1; }; function display(val) { document.write(val()); } var res = new func(11.98); display(res.display.bind(res)); </script> </body> </html>
In this tutorial, we have learned two techniques to pass an object as a parameter in the JavaScript function. The first technique is to use an object whose arguments must have the same names as the Object property names. The second technique is to use the "this" function and the prototype method.
- Related Articles
- How to pass a json object as a parameter to a python function?
- How to pass a function as a parameter in Java
- How to pass an array as a URL parameter in Laravel?
- How to pass a 2D array as a parameter in C?
- How to pass arrays as function arguments in JavaScript?
- How to pass a jQuery event as a parameter in a method?
- How to pass a lambda expression as a method parameter in Java?
- How can we pass an empty string as a parameter to BIT_LENGTH() function and what would be returned by MySQL?
- How can we use a JavaScript function as an object?
- How to pass Python function as a function argument?
- How to pass an entire structure as an argument to function in C?
- How to pass entire array as an argument to a function in C language?
- Pass long parameter to an overloaded method in Java
- How to pass a dictionary as argument in Python function?
- How to define rest parameter for a function in JavaScript?
