题目1

image.png

  1. function Teacher() {
  2. this.student = 500;
  3. this.skill = ["JS", "JQ"];
  4. }
  5. var teacher = new Teacher();
  6. Student.prototype = teacher;
  7. function Student() { }
  8. var student = new Student();
  9. student.student++;
  10. student.skill.push("HTML", "CSS");
  11. console.log(student);
  12. console.log(teacher);

image.png
自类不能修改父类的值

image.png
image.png

综合题目

原型与this

  1. function Parent() {
  2. this.a = 1;
  3. this.b = [1, 2, this.a];
  4. this.c = { demo: 5 };
  5. this.show = function () {
  6. console.log(this.a, this.b, this.c.demo);
  7. }
  8. }
  9. function Child() {
  10. this.a = 2;
  11. this.change = function () {
  12. this.b.push(this.a);
  13. this.a = this.b.length;
  14. this.c.demo = this.a++;
  15. }
  16. }
  17. Child.prototype = new Parent();
  18. var parent = new Parent();//1 [1,2,1],5
  19. var child1 = new Child();//11 1,[1,2,1],5
  20. var child2 = new Child();//12 1,[1,2,1],5
  21. child1.a = 11;
  22. child2.a = 12;
  23. parent.show();//1 [1,2,1],5
  24. // 通过new Parent执行,
  25. child1.show();//11,[1,2,1],5
  26. child2.show();//12,[1,2,1],5
  27. child1.change();//5,[1,2,1,11],4
  28. child2.change();//6,[1,2,1,11,12],5
  29. parent.show();//1,[1,2,1],5
  30. child1.show();//5,[1,2,1,11,12],5
  31. child2.show();//6,[1,2,1,11,12],5

image.png
image.png


原型

image.png
全是true

image.png
instanceof会遍历原型链

image.png
全是true

image.png
image.png