Translate

ads

21 Jun 2019

JavaScript | Object Methods

JavaScript | Object Methods


Object ways in JavaScript may be accessed by mistreatment functions. Functions in JavaScript square measure hold on as property values. The objects may be referred to as while not mistreatment bracket ().

✪In a technique, ‘this’ refers to the owner object.
✪Additional data may be value-added together with the thing technique.

Syntax:


       objectName.methodName()         


Properties:

A function may be divided into different property values, which are then combined and returned together.

For Ex: Student function contains the properties:

✪name
✪class
✪section

Return Value: It returns methods/functions stored as object
properties.

<!DOCTYPE html>
<html>

<head>
<title>
JavaScript Object Methods
</title>
</head>

<body>
<h1>Sayed 360</h1>

<h3>JavaScript Object Method</h3>

<p>
studentDetail is a function definition,
it is stored as a property value.
</p>

<p id="sis"></p>

<script>

// Object creation
var student = {
name: "Sagor",
class : "12th",
section : "B",

studentDetails : function() {
return this.name + " " + this.class
+ " " + this.section + " ";
}
};

// Display object data
document.getElementById("sis").innerHTML
= student.studentDetails();
</script>
</body>

</html>

Output:


Example 2: This example use storing property values and accessing without bracket ().

<!DOCTYPE html>
<html>

<head>
<title>
JavaScript Object Methods
</title>
</head>

<body>
<h1>Sayed 360</h1>

<h3>JavaScript Object Method</h3>

<p>
studentDetail is a function definition,
it is stored as a property value.
</p>

<p>
Function definition is returned
if we don't use ().
</p>
<p id="sis"></p>

<script>

// Object creation
var student = {
name: "sagor",
class : "12th",
section : "A",

studentDetails : function() {
return this.name + " " + this.class
+ " " + this.section + " ";
}
};

// Display object data
document.getElementById("sis").innerHTML
= student.studentDetails;
</script>
</body>

</html>
Output:


Example 3: Using function definition as property value and accessing with additional details.



<!DOCTYPE html>
<html>

<head>
<title>
JavaScript Object Methods
</title>
</head>

<body>
<h1>Sayed360</h1>

<h3>JavaScript Object Method</h3>

<p>
studentDetail is a function definition,
it is stored as a property value.
</p>

<p id="sis"></p>

<script>

// Object creation
var student = {
name: "sagor",
class : "12th",
section : "A",

studentDetails : function() {
return this.name + " " + this.class
+ " " + this.section + " ";
}
};

// Display object data
document.getElementById("sis").innerHTML
= "STUDENT " + student.studentDetails();
</script>
</body>

</html>

Output:






Introduction to Object Oriented Programming in JavaScript


JavaScript :The awing Script


No comments:

Post a Comment