I want to print data of an object in JavaScript. By defining a function and calling that function, i'm able to do that. To call such function i'm using s.show() in my code. But what i want is... by just specifying s [ like document.write(s) ], i want to print the object. In Java language, when we try to print an object, the System.out.println() calls the toString() method on that object. Is there any way in JavaScript?
Please help. Thanks in Advance.
<script>
function stud(a,b,c)
{
this.one=a;
this.two=b;
this.three=c;
this.show=function toString()
{
document.write(a+","+b+","+c+"<br>");
}
}
var s=new stud("smile","laugh","cry");
s.show(); // this is working
document.write(s); // i want this also work same
</script>
console.log(s).document.write(JSON.stringify(s));