下面的JS代码并以了一个movie对象,并给对象定义了一个toString()方法
<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("Narni","Andrew Adamson"); document.write(narnia.toString()); </script>
输出结果如下:
title: Narni director: Andrew Adamson