深入JavaScript

深入JavaScript Day10 - 【重点】原型链、认识O

2022-01-17  本文已影响0人  望穿秋水小作坊

一、原型链、认识Object

1、对于下面代码,描述查找name的过程?(理解原型链是什么)

var obj = {};
console.log(obj.name);
image.png

2、var obj = {} 是一个快速创建对象的语法糖,语法原形是什么?

var obj = new Object()

3、顶层原型到底是谁?如何证明?为什么需要有顶层原型?

var obj = {};
console.log(obj.__proto__.__proto__); // null
console.log(obj.__proto__ === Object.prototype); // ture

image.png

4、Object的本质是什么?

console.log(typeof Object); // function
console.log(typeof Object.prototype); // object
console.log(Object.prototype.__proto__); // null

5、Object是所有类的父类吗?

image.png image.png

二、理解继承

1、【重点掌握】绘制下面代码对应的内存图,并且思考打印结果?

  this.name = "why";
}

Person.prototype.running = function () {
  console.log(this.name + " 在跑步~");
};

function Student() {}

var p = new Person();
Student.prototype = p;

Student.prototype.studying = function () {
  console.log(this.name + " 在学习~");
};

var stu1 = new Student();
stu1.no = 100;
stu1.name = "lsp";
console.log(stu1);
console.log(stu1.__proto__);
console.log(stu1.__proto__.__proto__);
console.log(stu1.__proto__ === p);
stu1.studying();
stu1.running();

var stu2 = new Student();
stu2.no = 200;
console.log(stu2);
stu2.studying();
stu2.running();
Person { no: 100, name: 'lsp' }
Person { name: 'why', studying: [Function (anonymous)] }
{ running: [Function (anonymous)] }
true
lsp 在学习~
lsp 在跑步~
Person { no: 200 }
why 在学习~
why 在跑步~
image.png image.png

2、上面我们实现了继承体系,但是上面体系存在两大问题?

3、如何解决上述问题呢?

function Person(name, friends) {
  this.name = name;
  this.friends = friends;
}

Person.prototype.running = function () {
  console.log(this.name + " 在跑步~");
};

function Student(name, no, friends) {
  Person.call(this, name, friends);
  this.no = no;
}

var p = new Person();
Student.prototype = p;

Student.prototype.studying = function () {
  console.log(this.name + " 在学习~");
};

var stu1 = new Student("lsp", 100, []);
stu1.friends.push("lucy");
console.log(stu1);
console.log(stu1.friends);
console.log(stu1.__proto__);
console.log(stu1.__proto__.__proto__);
console.log(stu1.__proto__ === p);
stu1.studying();
stu1.running();

var stu2 = new Student("why", 200, []);
console.log(stu2.friends);
console.log(stu2);
stu2.studying();
stu2.running();
Person { name: 'lsp', friends: [ 'lucy' ], no: 100 }
[ 'lucy' ]
Person {
  name: undefined,
  friends: undefined,
  studying: [Function (anonymous)]
}
{ running: [Function (anonymous)] }
true
lsp 在学习~
lsp 在跑步~
[]
Person { name: 'why', friends: [], no: 200 }
why 在学习~
why 在跑步~
image.png image.png

4、上述【借用构造函数】存在2大缺点?

image.png

5、JavaScript实现继承的完整版思路和方案?

image.png image.png
上一篇 下一篇

猜你喜欢

热点阅读