|
@@ -0,0 +1,39 @@
|
|
|
+function Person(name,age) {
|
|
|
+ this.name = name;
|
|
|
+ this.age = age;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 所有构造函数 都有一个prototype属性 这个属性指向它的原型对象
|
|
|
+ * 原型对象的特点:声明在原型对象下的属性和方法 都能被实例化对象所共享
|
|
|
+ * 属性 构造函数下
|
|
|
+ * 方法 原型对象下
|
|
|
+ */
|
|
|
+
|
|
|
+Person.prototype.eat = function() {
|
|
|
+ console.log("eat");
|
|
|
+}
|
|
|
+
|
|
|
+var p1 = new Person();
|
|
|
+p1.eat();
|
|
|
+
|
|
|
+// 构造函数constructor
|
|
|
+console.log(Person.prototype.constructor);
|
|
|
+
|
|
|
+/**
|
|
|
+ * 继承父类的方法
|
|
|
+ * 在子类构造函数里 通过调用父类的.call去继承
|
|
|
+ * 子类的继承对象 = new 父类 继承方法
|
|
|
+ */
|
|
|
+
|
|
|
+function fn1(name,age) {
|
|
|
+ // this 当前对象
|
|
|
+ Person.call(this,name,age)
|
|
|
+}
|
|
|
+/**继承父类的方法 */
|
|
|
+fn1.prototype= new Person();
|
|
|
+
|
|
|
+fn1.prototype.constructor = fn1;
|
|
|
+var p2 = new fn1("HH",20);
|
|
|
+console.log(p2,'p2');
|
|
|
+p2.eat();
|