123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <!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>
- /**
- * 构造函数
- * new 实例化对象
- * this指向当前对象本身
- * 首字母大写
- * 自带了prototype 原型对象 和 constructor 构造器
- *
- * 属性 写在构造函数下
- * 方法 写在原型对象下
- *
- */
- function Person(name,age) {
- console.log(this);
- this.name = name;
- this.age = age;
- }
- Person.prototype.eat = function() {
- console.log("吃粽子")
- }
- Person.prototype.drink = function() {
- console.log("喝甜水")
- }
- var p1 = new Person('Lucy',20);
- console.log(p1.name,p1.age);
- p1.eat();
- p1.drink();
- p1.name = '哈哈';
- console.log(p1);
-
- /**
- * 原型
- * 1.构造函数中自带了constructor(构造器)和prototype(原型对象/显式的方法)
- * 2.constructor 指向的是 prototype 的构造函数
- * 3.__proto__和prototype是等价的
- */
- /**
- * 原型链:
- * 访问对象属性时 先上对象本身的属性去找
- * 通过__proto__(原型自带的一个隐式方法)去构造函数上找
- * 若还未找到 则在原型对象prototype上去找 若在为找到 则返回null
- */
- </script>
- </body>
- </html>
|