
- 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
Using a JavaScript object inside a static() method?
Actually, the result will be in vain when we try to use an object inside a static method. But when the object is sent as a parameter, we can access the object. let's discuss it in a nutshell.
Example-1
In the following example, we tried to use the object "myComp" directly rather than sending it as a parameter, therefore, we get no result. If we open the browser console we will get an error that is "myComp.comp() is not a function". To get the actual result we have to send the object as a parameter as shown in Example-2
<html> <body> <p id="method"></p> <script> class Company { constructor(branch) { this.name = branch; } static comp() { return "Tutorix is the best e-learning platform" } } myComp = new Company("Tesla"); document.getElementById("method").innerHTML = myComp.comp(); </script> </body> </html>
Example-2
In the following example, the object is sent as a parameter. Therefore we will get as shown in the output.
<html> <body> <p id="method"></p> <script> class Company { constructor(branch) { this.name = branch; } static comp(val) { return "Elon musk is the head of " + val.name } } myComp = new Company("Tesla"); document.getElementById("method").innerHTML = Company.comp(myComp); </script> </body> </html>
Output
Elon musk is the head of Tesla
- Related Articles
- Reference to a static method using method references in Java8
- Add a method to a JavaScript object constructor?
- Parsing array of objects inside an object using maps or forEach using JavaScript?
- How to add a method to a JavaScript object?
- How to call a non-static method of an abstract class from a static method in java?
- How to edit values of an object inside an array in a class - JavaScript?
- How do I look inside a Python object?
- How to create a Number object using JavaScript?
- How to create a URL object using JavaScript?
- How to use a variable inside a Foreach-Object Parallel?
- Java static method
- Static vs. Non-Static method in C#
- What are the restrictions imposed on a static method or a static block of code in java?
- Create a heading inside the media object with Bootstrap
- How to change the value of a global variable inside of a function using JavaScript?

Advertisements