-1

代码片段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 却没有呢?

4

1 回答 1

3

在 中codes 1,你有这个:

console.log(arguments);

这可以在这里找到:

  ...
  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];
  }
  ...

此代码将参数值写入console. 由于代码不存在于 中codes 2,因此它不会在控制台中输出任何内容。

于 2013-06-17T02:15:20.160 回答