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.


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

Try it:

JS Bin on

Related topics:

Sign Up/ Sign In
Ask a Question

Copyright © 2018 CodesJava Protection Status SiteMap Reference: Java Wiki