下面的JS代码通过function定义了一个movie对象,在movie对象内定义了一个toString方法,toString方法通过外部函数实现。
<script type="text/javascript"> function movieToString() { return("title: "+this.title+" director: "+this.director); } function movie(title, director) { this.title = title; this.director = director; this.toString = movieToString; //assign function to this method pointer } var aliens = new movie("Aliens","Cameron"); document.write(aliens.toString()); </script>