1.原型链继承

1.1 通过操作构造函数的prototype属性可以实现继承

  1. Professor.prototype = {
  2. name: 'Mr. zhang',
  3. tSkill: 'JAVA'
  4. }
  5. function Professor(){}
  6. var professor = new Professor();
  7. Teacher.prototype = professor;
  8. function Teacher(){
  9. this.name = 'Mr. wang',
  10. this.mSkill = 'JS/JQ'
  11. }
  12. var teacher = new Teacher();
  13. Student.prototype = teacher;
  14. function Student(){
  15. this.name = 'Mr. Li',
  16. this.pSkill = 'HTML/CSS'
  17. }
  18. var student = new Student();
  19. console.log(student);

image.png

1.2 call/apply只是在借用Teacher()属性而不是继承

  1. function Teacher(name, mSkill){
  2. this.name = name;
  3. this.mSkill = mSkill;
  4. }
  5. function Student(name, mSkill, age, major) {
  6. Teacher.apply(this, [name, mSkill]);
  7. this.age = age;
  8. this.major = major;
  9. }
  10. var student = new Student(
  11. 'Mr.zhang', 'JS/JQ', 18, 'Computer'
  12. );
  13. console.log(student);

image.png

2. 圣杯模式

2.1 企业级方法 -> Buffer初级

  1. function Teacher(){
  2. this.name = 'Mr.Li';
  3. this.tSkill = 'JAVA';
  4. }
  5. Teacher.prototype = {
  6. pSkill: 'JS/JQ';
  7. }
  8. var t = new Teacher();
  9. function Student(){
  10. this.name = 'Mr. wang';
  11. }
  12. function Buffer(){}
  13. Buffer.prototype = Teacher.prototype;
  14. var buffer = new Buffer();
  15. Student.prototype = buffer;
  16. Student.prototype.age = 18;
  17. var s = new Student();
  18. console.log(s);
  19. console.log(s.pSkill);
  20. console.log(Student.prototype, Teacher.prototype);

image.png

2.2 标准写法

  1. Teacher.prototype.name = 'Mr. zhang';
  2. function Teacher(){}
  3. function Student(){}
  4. function Buffer(){}
  5. inherit(Student, Teacher);
  6. var s = new Student();
  7. var t = new Teacher();
  8. // inherit继承
  9. function inherit(Target, Origin){
  10. function Buffer(){}
  11. Buffer.prototype = Origin.prototype;
  12. Target.prototype = new Buffer();
  13. // 构造器指向自己
  14. Target.prototype.constructor = Target;
  15. // 设置继承源
  16. Target.prototype.super_class = Origin;
  17. }

2.3 企业级写法(模块化开发IIFE)

  1. var inherit = (function(){
  2. // 1.声明一个Buffer构造函数
  3. var Buffer = function(){}
  4. return fucntion (Target, Origin){
  5. // 2.将被继承对象Origin的原型赋值给Buffer构造函数的原型
  6. Buffer.prototype = Origin.prototype;
  7. // 3.将实例化Buffer构造函数,作为继承对象Target的原型
  8. Target.prototype = new Buffer();
  9. // 构造器指向自己
  10. Target.prototype.constructor = Target;
  11. // 设置继承源
  12. Target.prototype.super_class = Origin;
  13. }
  14. })();
  15. Teacher.prototype.name = 'Mr.zhang';
  16. function Teacher(){}
  17. function Student(){}
  18. inherit(Student, Teacher);
  19. Student.prototype.age = 18;
  20. var s = new Student();
  21. var t = new Teacher();
  22. console.log(s);
  23. console.log(t);

image.png

3. CSS圣杯模式 双飞翼

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4. <title>Document</title>
  5. <style>
  6. .clearfix::after {
  7. content: "";
  8. display: block;
  9. clear: both;
  10. }
  11. .wrap {
  12. margin: 0 auto;
  13. width: 700px;
  14. border: 1px solid #000;
  15. }
  16. .top,
  17. .foot {
  18. height: 50px;
  19. background-color: #000;
  20. }
  21. .main {
  22. padding: 0 100px;
  23. overflow: hidden;
  24. }
  25. .main .left,
  26. .main .content,
  27. .main .right {
  28. float: left;
  29. position: relative;
  30. margin-bottom: -2000px;
  31. padding-bottom: 2000px;
  32. background-color: green;
  33. }
  34. .main .left {
  35. left: -100px;
  36. width: 100px;
  37. }
  38. .main .content {
  39. width: 100%;
  40. margin-left: -100px;
  41. background-color: red;
  42. }
  43. .main .right {
  44. margin-left: -100px;
  45. left: 100px;
  46. width: 100px;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="wrap">
  52. <div class="top"></div>
  53. <div class="main clearfix">
  54. <div class="left">123</div>
  55. <div class="content">234<br />123</div>
  56. <div class="right">123</div>
  57. </div>
  58. <div class="foot"></div>
  59. </div>
  60. </body>

4. 模块化

  • 优点
    • 防止全局污染
    • 利于后期的维护
    • 二次开发 ```javascript function inherit(Target, Origin){ function Buffer(){} Buffer.prototype = Origin.prototype; var buffer = new Buffer(); Target.prototype = buffer; Target.prototype.constructor = Target; Target.prototype.super_class = Origin; }

var initProgrammer = (function(){ var Programmer = function(){} Programmer.prototype = { name: ‘程序员’, tool: ‘计算机’, work: ‘编写应用程序’, duration: ‘10个小时’, say: function(){ console.log(‘我是一名’ + this.myName + this.name + ‘, 我的工作是用’ + this.tool + this.work + ‘, 我每天工作’ + this.duration + ‘,我的工作需要用到’ + this.lang.toString() + ‘.’ ); } }

function FrontEnd(){} function BackEnd(){}

inherit(FrontEnd, Programmer); inherit(BackEnd, Programmer);

FrontEnd.prototype.myName = ‘前端’; FrontEnd.prototype.lang = [‘HTML’, ‘CSS’, ‘JavaScript’];

BackEnd.prototype.lang = [‘Node’, ‘Java’, ‘SQL’]; BackEnd.prototype.myName = ‘后端’;

return { FrontEnd: FrontEnd, BackEnd: BackEnd } })()

var frontEnd = new initProgrammer.FrontEnd(); var backEnd = new initProgrammer.BackEnd();

frontEnd.say(); backEnd.say();

<a name="Rl2Pq"></a>
# 5. 作业
运用模块化开发的方法实现:

1. 打印一个参数值以内,参数能被3/5/7整除的数
1. 打印斐波那契数列的第N位
1. 打印从0到一个数的累加值
```javascript
var initDiv = (function(){
    function div(num){
      var arr = [];
      for(var i = 0; i <= num; i++){
        if(i % 3 === 0 || i % 5 === 0 || i % 7 === 0){
          arr.push(i);
        }
      }
      return arr
    }
    return div
  })()

  // console.log(initDiv(100));

  // 1 1 2 3 5 8
  var initFibinacci = (function(){
    var num1 = 1;
    var num2 = 1;
    function fib(n){
      if(n === 1){
        return 1
      }
      if(n === 2){
        return 1
      }
      if(n > 2){  
        return fib(n - 1) + fib(n - 2)
      }
    }
    return fib
  })()

  // var res = initFibinacci(6);
  // console.log(res);

  // 1 2 3 4 5
var cumulative = (function(){
  var sum = 0;
  function add(n){
    for(var i = 0; i <= n; i++){
      sum += i;
    }
    return sum
  }
  return add
})()