我想用JavaScript打印對象的數據。通過定義一個函數並調用該函數,我可以做到這一點。要調用這樣的功能,我在我的代碼中使用了s.show()。但我想要的是...只需指定s [如document.write(s)],我想打印該對象。在Java語言中,當我們嘗試打印一個對象時,System.out.println()在該對象上調用toString()方法。 JavaScript有什麼方法嗎?用javascript打印對象與我們用Java打印對象的方式相似
請幫忙。提前致謝。
<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));' –
JSON.stringify()顯示對象的**屬性**和**值**但我只想要打印的值。任何幫助? – PoornaChandra