19_构造函数.html 854 B

123456789101112131415161718192021222324252627282930
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 构造函数
  11. var arr = new Array(1,2,3,4);
  12. var str = new String('1234');
  13. function Person(userName,userAge){
  14. this.userName = userName;
  15. this.userAge = userAge;
  16. this.talk = function(){
  17. console.log(`大家好我叫${this.userName}今年${this.userAge}岁了`);
  18. }
  19. }
  20. var person1 = new Person('张三',18);
  21. console.log(person1.userName);
  22. console.log(person1.userAge);
  23. person1.talk();
  24. var person2 = new Person('李四',20);
  25. console.log(person2.userName);
  26. person2.talk();
  27. </script>
  28. </body>
  29. </html>