JS - Constructor还可以这样用

JS中Constructor好用法:

即在只知道一个对象实例的情况下(不知道对象名),重新初始化一个新实例;

 1 function Person( firstname, lastname, age ) {
 2   this.firstname = firstname;
 3   this.lastname = lastname;
 4   this.age = age;
 5 }
 6 
 7 var dave = new Person("Dave", "Smith", 28);
 8 
 9 document.write( dave.constructor + "<br />");
10 // function Person(firstname, lastname, age) { this.firstname = firstname; this.lastname = lastname; this.age = age; }
11 
12 // we can create a new object from another object's constructor:
13 var mike = new dave.constructor("Mike", "Fox", 22);
14 
15 // Is mike an instance of the Person object?
16 document.write( mike.constructor == Person );    // true

 

posted on 2015-08-11 15:51  tx天翔  阅读(220)  评论(0编辑  收藏  举报