In this post let us see how to create a
javascript function with parameter and associated function
How to create object in javascript programming
language .
Step 1:
Let us use Notepad++
as our editor, we create new html page and save is as what name you like with
.html extension.
Here is the sample i created for the demo.
Step 2:
Create new html page.
<!DOCTYPE html>
<html lang="en">
<head>
<title>:::JAVASCRIPT OBJECTS:::</title>
</head>
<body>
<script type="text/javascript">
//javascript function with two parameters,
//constructor function
function showDetails(name, age)
{
//properties
this.name = name;
this.age = age;
//Assign the showSalary function sal properties member
this.sal = showSalary;
}
//Associate function
function showSalary(amount)
{
var salary = amount * 3;
return salary;
}
//Creating object for the function showDetails
var emp = new showDetails("Pradeep", 24);
//Displaying the name into the browser
document.write("My name is : <b>" + emp.name + "</b><br />");
//Displaying the age into the browser
document.write("My age is : <b>" + emp.age + "</b><br />");
//passing parameter to the showSalary function this is the way we need to call he function in javascript.
document.write("My Salary is : <b>" + emp.sal(5000) + "</b>");
</script>
</body>
</html>
Finally execute the program
in the browser and see output. Thank you
Output
No comments:
Post a Comment