1.原型链继承
1.1 通过操作构造函数的prototype属性可以实现继承
Professor.prototype = {name: 'Mr. zhang',tSkill: 'JAVA'}function Professor(){}var professor = new Professor();Teacher.prototype = professor;function Teacher(){this.name = 'Mr. wang',this.mSkill = 'JS/JQ'}var teacher = new Teacher();Student.prototype = teacher;function Student(){this.name = 'Mr. Li',this.pSkill = 'HTML/CSS'}var student = new Student();console.log(student);
1.2 call/apply只是在借用Teacher()属性而不是继承
function Teacher(name, mSkill){this.name = name;this.mSkill = mSkill;}function Student(name, mSkill, age, major) {Teacher.apply(this, [name, mSkill]);this.age = age;this.major = major;}var student = new Student('Mr.zhang', 'JS/JQ', 18, 'Computer');console.log(student);
2. 圣杯模式
2.1 企业级方法 -> Buffer初级
function Teacher(){this.name = 'Mr.Li';this.tSkill = 'JAVA';}Teacher.prototype = {pSkill: 'JS/JQ';}var t = new Teacher();function Student(){this.name = 'Mr. wang';}function Buffer(){}Buffer.prototype = Teacher.prototype;var buffer = new Buffer();Student.prototype = buffer;Student.prototype.age = 18;var s = new Student();console.log(s);console.log(s.pSkill);console.log(Student.prototype, Teacher.prototype);
2.2 标准写法
Teacher.prototype.name = 'Mr. zhang';function Teacher(){}function Student(){}function Buffer(){}inherit(Student, Teacher);var s = new Student();var t = new Teacher();// inherit继承function inherit(Target, Origin){function Buffer(){}Buffer.prototype = Origin.prototype;Target.prototype = new Buffer();// 构造器指向自己Target.prototype.constructor = Target;// 设置继承源Target.prototype.super_class = Origin;}
2.3 企业级写法(模块化开发IIFE)
var inherit = (function(){// 1.声明一个Buffer构造函数var Buffer = function(){}return fucntion (Target, Origin){// 2.将被继承对象Origin的原型赋值给Buffer构造函数的原型Buffer.prototype = Origin.prototype;// 3.将实例化Buffer构造函数,作为继承对象Target的原型Target.prototype = new Buffer();// 构造器指向自己Target.prototype.constructor = Target;// 设置继承源Target.prototype.super_class = Origin;}})();Teacher.prototype.name = 'Mr.zhang';function Teacher(){}function Student(){}inherit(Student, Teacher);Student.prototype.age = 18;var s = new Student();var t = new Teacher();console.log(s);console.log(t);
3. CSS圣杯模式 双飞翼
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.clearfix::after {content: "";display: block;clear: both;}.wrap {margin: 0 auto;width: 700px;border: 1px solid #000;}.top,.foot {height: 50px;background-color: #000;}.main {padding: 0 100px;overflow: hidden;}.main .left,.main .content,.main .right {float: left;position: relative;margin-bottom: -2000px;padding-bottom: 2000px;background-color: green;}.main .left {left: -100px;width: 100px;}.main .content {width: 100%;margin-left: -100px;background-color: red;}.main .right {margin-left: -100px;left: 100px;width: 100px;}</style></head><body><div class="wrap"><div class="top"></div><div class="main clearfix"><div class="left">123</div><div class="content">234<br />123</div><div class="right">123</div></div><div class="foot"></div></div></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
})()
