|
@@ -0,0 +1,42 @@
|
|
|
+<!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>
|
|
|
+ /**
|
|
|
+ * 所有构造函数下都有两个属性:constructor 和 prototype
|
|
|
+ * 原型的特点:
|
|
|
+ * 1.方法 写在原型下
|
|
|
+ * 2.属性 写在构造函数下
|
|
|
+ *
|
|
|
+ */
|
|
|
+ function Person(name,age) {
|
|
|
+ this.name = name;
|
|
|
+ this.age = age;
|
|
|
+
|
|
|
+ Person.prototype.eat = function() {
|
|
|
+ console.log("青草蛋糕")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let p1 = new Person('LiLi',20);
|
|
|
+ console.log(p1);
|
|
|
+ p1.eat();
|
|
|
+ /**
|
|
|
+ * 1.令当前子函数的原型 是 当前实例化的父函数
|
|
|
+ * 2.父对象.call 修改this指向
|
|
|
+ */
|
|
|
+ function Child(name,age,a) {
|
|
|
+ Person.call(this,name,age);
|
|
|
+ this.a =a;
|
|
|
+ }
|
|
|
+ Child.prototype = new Person();
|
|
|
+ let aa = new Child('HAHA',10,'篮球');
|
|
|
+ console.log(aa)
|
|
|
+ aa.eat();
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+</html>
|