代码片段1:
<script>
function Person(lastName, firstName){
this.lastName = lastName;
this.firstName = firstName;
}
var DnnyGdmn = new Person("Goodman","Danny");
var DvdFlngn = new Person("Flanagan","David");
function Book(title, pages, price){
this.title = title;
this.pages = pages;
this.price = price;
this.authors = new Array(arguments.length-3);
console.log(arguments);
for(i = 0; i < arguments.length - 3; i++){
this.authors[i] = arguments[i + 3];
}
}
var JavaNut = new Book("Java Foundation Classes in a Nutshell", 731, 29.95, DvdFlngn);
var JSTDR = new Book("Javascript: The Definitive Guide (3rd Edition)", 776, 39.95, DvdFlngn);
</script>
在 Firefox firebug->console->all 中,我看到:
["Java Foundation Classes in a Nutshell", 731, 29.95, Person { lastName="Flanagan", firstName="David"}]
["Javascript: The Definitive Guide (3rd Edition)", 776, 39.95, Person { lastName="Flanagan", firstName="David"}]
代码片段2:
<script>
function Person(lastName, firstName){
this.lastName = lastName;
this.firstName = firstName;
}
var DnnyGdmn = new Person("Goodman","Danny");
var DvdFlngn = new Person("Flanagan","David");
</script>
在 Firefox firebug->console->all 中,它不显示任何内容。
问题:
两个代码片段都做同样的事情,创建某个对象的实例,但是为什么 1 向控制台写入内容,而代码 2 却没有呢?