Table of Contents
Methods are actions that can be performed on objects. A method is a function stored as a property of an object:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>What are Object Methods?</h4>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
document.getElementById("demo").innerHTML =
"Method result: " + person.fullName();
</script>
</body>
</html>

Result View Example
In object methods, this refers to the owner object. Without this, a function cannot access object properties:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The this Keyword</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
introduce: function() {
return "Hi, I'm " + this.firstName + " " + this.lastName;
},
getAge: function() {
return "I am " + this.age + " years old.";
}
};
document.getElementById("demo1").innerHTML = person.introduce();
document.getElementById("demo2").innerHTML = person.getAge();
</script>
</body>
</html>

Result View Example
Object methods can be accessed with or without parentheses. Without parentheses, you get the function definition:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Accessing Object Methods</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
// Access method WITH parentheses
document.getElementById("demo1").innerHTML =
"With parentheses: " + person.fullName();
// Access method WITHOUT parentheses (returns function)
document.getElementById("demo2").innerHTML =
"Without parentheses: " + person.fullName;
</script>
</body>
</html>

Result View Example
You can add methods to an existing object:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Adding a Method to an Object</h4>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe"
};
// Add a method
person.fullName = function() {
return this.firstName + " " + this.lastName;
};
// Add another method
person.greet = function(name) {
return "Hello " + name + ", I'm " + this.firstName;
};
document.getElementById("demo").innerHTML =
person.fullName() + "<br>" +
person.greet("Jane");
</script>
</body>
</html>

Result View Example
Object methods can perform calculations using object properties:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Using Methods for Calculations</h4>
<p id="demo"></p>
<script>
const rectangle = {
width: 10,
height: 5,
area: function() {
return this.width * this.height;
},
perimeter: function() {
return 2 * (this.width + this.height);
},
isSquare: function() {
return this.width === this.height;
}
};
document.getElementById("demo").innerHTML =
"Width: " + rectangle.width + "<br>" +
"Height: " + rectangle.height + "<br>" +
"Area: " + rectangle.area() + "<br>" +
"Perimeter: " + rectangle.perimeter() + "<br>" +
"Is square? " + rectangle.isSquare();
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.