Ask coding questions

← Back to all posts
Help With Methods In JavaScript
TheCoiledViper (87)

I'm very new to JavaScript and am trying to learn about class methods and the call() method. Could anyone help me fix my code?

https://repl.it/@TheCoiledViper/JavaScript-Methods

*Update I have fixed the issue by adding 'static' in front of the class method.

Comments
hotnewtop
shubhrapandit (0)

Hey
JavaScript Methods
JavaScript methods are actions that can be performed on objects.

A JavaScript method is a property containing a function definition.

Property Value
firstName John
lastName Doe
age 50
eyeColor blue
fullName function() {return this.firstName + " " + this.lastName;}

Accessing Object Methods
You access an object method with the following syntax:

objectName.methodName()
You will typically describe fullName() as a method of the person object, and fullName as a property.

The fullName property will execute (as a function) when it is invoked with ().

This example accesses the fullName() method of a person object:

Example
name = person.fullName();
Adding a Method to an Object
Adding a new method to an object is easy:

Example
person.name = function () {
return this.firstName + " " + this.lastName;
};
Using Built-In Methods
This example uses the toUpperCase() method of the String object, to convert a text to uppercase:

let message = "Hello world!";
let x = message.toUpperCase();
The value of x, after execution of the code above will be:

HELLO WORLD!
Example
person.name = function () {
return (this.firstName + " " + this.lastName).toUpperCase();
};

To know more visit Java Training in Pune (https://bestjavaclassesinpune.com)