
How to associate functions with objects using JavaScript?

Let's now create a custom "toString()" method for our movie object. We can embed the function directly in the object like this.
<script type="text/javascript">
function movie(title, director) {
this.title = title;
this.director = director;
this.toString = function movieToString() {
return("title: "+this.title+" director: "+this.director);
}
}
var narnia = new movie("Narnia","Andrew Adamson");
document.write(narnia.toString());
</script>
This produces
title: Narnia director: Andrew Adamson
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.