123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- /*
- 1.构造函数可以创建实例化对象
- 2.属性写在 构造函数里
- 方法写在 原型对象下
- */
- //构造函数
- function person(name, age) {
- this.name = name
- this.age = age
- // this.eat = function () {
- // console.log(this.name + '我在吃饭')
- // }
- }
-
- //原型对象
- person.prototype.eat = function(){
- console.log(this.name + '我在吃饭')
- }
- /*
- 所有的构造函数都有一个prototype属性
- 这个prototype属性 指向的都是原型对象
- */
- //实例化对象
- var p1 = new person('zs', 18)
- var p2 = new person('lisi', 20)
- console.log(p1)
- console.log(p2)
- p1.eat()
- p2.eat()
- </script>
- </body>
- </html>
|