CodesJava

Easy learning with example program codes

How to add method to javascript object?


How to add method to javascript object:

We can add a method to JavaScript object. First define a method and then assign that method as a property to object.

Example:

<html>
<body>
<script> 
function stuClassName(className){  
this.className=className;     
}  
function Student(name,rollNo){  
this.name=name;  
this.rollNo=rollNo; 
this.stuClassName= stuClassName;  
}  
var student=new Student("Jai", "MCA/07/06"); 
student.stuClassName("MCA Final");
document.write("Name: " +student.name + "</br>");  
document.write("RollNo: " + student.rollNo + "</br>");  
document.write("Class: " + student.className);      
</script>  
</body>
</html>

Try it:

JS Bin on jsbin.com
 

Related topics:

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki