1234567891011121314151617181920212223242526272829303132333435 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- // 构造函数
- // 构造函数的首字母一般大写
- // this 指向构造函数
- function Person(name,age){
- this.username = name;
- this.age = age;
- // this.talk = function(){
- // console.log(`我叫${this.username},我今年${this.age}岁`)
- // }
- }
- // 原型上写方法
- Person.prototype.talk = function(){
- console.log(`我叫${this.username},我今年${this.age}岁`)
- }
- // 实例化对象
- let p1 = new Person("张三",18);
- let p2 = new Person("李四",20);
- console.log(p1);
- console.log(p2);
- p1.talk();
- p2.talk();
-
- </script>
- </body>
- </html>
|