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

 

相关文章:

  • 2021-10-15
  • 2021-08-02
  • 2021-10-01
  • 2021-05-21
  • 2021-06-30
  • 2021-11-02
  • 2021-12-02
  • 2022-01-09
猜你喜欢
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2022-02-23
  • 2021-12-07
  • 2021-07-30
相关资源
相似解决方案