一、原型和原型链总结
- 原型是构造函数身上的一个属性,构造函数实例化以后就可以继承原型上的公有属性和方法
- 原型是定义构造函数所构造出实例对象的公共祖先
- 所有被该构造函数构造出的实例对象都会继承原型上的属性和方法
- 原型上有一个constructor属性,它指向的是当前这个构造器,也就是构造函数本身
- 原型的最顶端是Object.prototype
原型其实也是一个对象,原型的原型一定是有Object构造出来的
// 原型和原型链总结// 原型是构造函数身上的一个属性,构造函数实例化以后可以继承原型上的属性和方法// 原型是定义构造函数所构造出每一个实例对象的公共祖先// 所有被该构造函数所构造出的实例化对象都会继承原型上的属性和方法// 原型也是对象,原型的原型一定是由Object所构造出来的// 原型的最顶端是Object.prototype// 构造函数原型上有一个属性,constructor,指向的是构造器,也就是当前这个构造函数本身function Person(){}Person.prototype.name = 'Lucy';var p = new Person();console.log(p);
二、几种对象继承的方式
2.1、原型链继承
缺点:会继承祖先身上的的所有属性和方法,同时也会继承祖先原型链上的所有属性和方法
// 对象的几种继承方式// 原型链继承:缺点就是会继承祖先身上的所有的属性和方法,同时也会继承祖先原型链上所有的属性和方法Professor.prototype = {name: '张教授',tSkill: 'JAVA'}function Professor(){}var professor = new Professor();Teacher.prototype = professor;function Teacher(){this.name = '王老师';this.mSkill = 'JS/JQ';}var teacher = new Teacher();Student.prototype = teacher;function Student(){this.name = '李同学';this.pSkill = 'HTML/CSS';}var student = new Student();console.log(student);
2.2、call和apply继承
缺点:call和apply继承,严格意义上来说只是通过改变this指向来借用方法或者是属性;它的缺点是无法继承原型上的属性或者是方法
- call和apply两者都可以改变其this指向
- call传递参数是一项一项传递的
apply传递参数是以数组的形式传递的
// call和apply继承// call和apply继承,严格意义上来说只是通过改变其this指向,来借用属性或者方法;它不能继承原型上的属性或者是方法Teacher.prototype.wife = '李女士';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('王先生', 'JS/JQ', 18, 'computer');console.log(student);
2.3、公共原型的方法
缺点:只要其中一个原型更改了属性,那么被关联的原型上的属性就会受到影响
// 公共原型继承法// 只要其中一个原型更改了属性,被关联的原型上的属性就会受到影响function Teacher(){this.name = '王老师';this.tSkill = 'JAVA';}Teacher.prototype = {pSkill: 'JS/JQ'}var teacher = new Teacher();console.log(teacher);function Student(){this.name = '李同学';}Student.prototype = Teacher.prototype;Student.prototype.age = 20;var student = new Student();console.log(student);console.log(teacher);console.log(Teacher.prototype);
2.4、圣杯模式解决继承所产生的问题「企业级解决方案」
原理:定义一个中间件「空的构造函数」;让中间件这个构造函数的原型等于需要被继承的这个构造函数的原型;然后实例化这个中间件对象,最后让需要继承的这个构造函数的原型等于被实例出来的这个中间件的对象

// 圣杯模式继承原型function Teacher(){this.name = '王老师';this.tSkill = 'JAVA';}Teacher.prototype = {pSkill: 'JS/JQ'}var teacher = new Teacher();console.log(teacher);function Student(){this.name = '李同学';}// 定义中空的间件function Buffer(){}// 公共原型一定要在实例化对象之前Buffer.prototype = Teacher.prototype;var buffer = new Buffer();Student.prototype = buffer;Student.prototype.age = 22;var student = new Student();console.log(student.pSkill);console.log(student);
2.4.1、圣杯模式初级封装
// 圣杯模式初级封装Teacher.prototype.name = '颜老师';function Teacher(){}function Student(){}inherit(Student, Teacher);// 这部分每次都需要使用,每次写代码就会过于冗余;不利用后期维护// function Buffer(){}// Buffer.prototype = Teacher.prototype;// var buffer = new Buffer();// Student.prototype = buffer;function inherit(Target, Origin){function Buffer(){}Buffer.prototype = Origin.prototype;Target.prototype = new Buffer();// 还原构造器指向Target.prototype.constructor = Target;// 定义原始的继承类Target.prototype.supper_class = Origin;}var teacher = new Teacher();var student = new Student();console.log(student);console.log(teacher);console.log(student.name);
2.4.2、复习闭包
// 复习闭包// 普通闭包function test(){var num = 1;function add(){num++;console.log(num);}return add;}var add = test();add();add();add();// 普通闭包function test(){var num = 1;var compute = {add: function(){num++;console.log(num);},minus: function(){num--;console.log(num);}}return compute;}var compute = test();compute.add();compute.minus();// 构造函数也能形成闭包function Compute(){// 在实例化对象的时候系统隐式创建this对象// var this = {// add: function(){...}// minus: function(){...}// }var num = 0;this.add = function(){num++;console.log(num);}this.minus = function(){num--;console.log(num);}// 系统会隐式返回this// return this;}var compute = new Compute();compute.add();compute.minus();
2.4.3、圣杯模式进阶封装
// 需要在全局定义变量,并且需要手动去执行所封装的函数var inherit = initInherit();Teacher.prototype.name = '颜老师';function Teacher(){}function Student(){}inherit(Student, Teacher);var teacher = new Teacher();var student = new Student();console.log(student);console.log(teacher);console.log(student.name);// 圣杯模式终极封装function initInherit(){var Buffer = function(){}function inherit(Target, Origin){Buffer.prototype = Origin.prototype;Target.prototype = new Buffer();// 还原构造器Target.prototype.constructor = Target;Target.prototype.supper_class = Origin;}return inherit;}
2.4.4、圣杯模式终极封装
// 圣杯模式终极封装// 使用模块化思想来进行封装var inherit = (function(){var Buffer = function(){}return function(Target, Origin){Buffer.prototype = Origin.prototype;Target.prototype = new Buffer();// 还原构造器Target.prototype.constructor = Target;// 定义终极继承的超类Target.prototype.supper_class = Origin;}})();Teacher.prototype.name = '颜老师';function Teacher(){}function Student(){}inherit(Student, Teacher);var teacher = new Teacher();var student = new Student();console.log(student);console.log(teacher);console.log(student.name);
三、模块化开发
3.1、按需启动
按需启动,不让它立即执行,那就交给一个全局变量让它等待执行即可
// 模块化开发// 按需启动,不让它立即执行,把它交给一个全局变量让其等待执行// 页面一加载就执行window.onload = function(){init();}// 初始化函数function init(){initCompute();initFuncs();}// // 功能函数,模块化var initCompute = (function(){var a = 1,b = 2;this.add = function(){return a + b;}this.minus =function(){return a - b;}this.mul =function(){return a * b;}this.div = function(){return a / b;}return function(){add();minus();mul();div();}})();// 功能函数,模块化var initFuncs = (function(){})();// 功能函数,模块化var initFuncs = (function(){})();
3.2、插件化开发
插件化开发,页面加载的时候就需要执行
// 插件化开发// 页面加载的时候就需要执行;function(){var Slider = function(){}// function Slider(){}Slider.prototype = {}window.Slider = Slider;}();var slider = new Slider({// 传入配置});
四、作业
把三个JS文件合并成一个
1、打印一个参数值能够同时被3或者是5或者是7同时整除的数
2、打印斐波拉契数列
3、打印从0到一个数的累加值
var initCompute = (function () {// 传统闭包的模式// return function (agr) {// if (agr % 2 === 0 || agr % 3 === 0 || agr % 5 === 0) {// console.log('这个数能够同时被2或者是3或者是5整除');// }else{// console.log('小主,这个数不能够同时被2或者是3或者是5整除哦!');// }// }this.compute = function (agr) {if (agr % 2 === 0 || agr % 3 === 0 || agr % 5 === 0) {console.log('这个数能够同时被2或者是3或者是5整除');} else {console.log('小主,这个数不能够同时被2或者是3或者是5整除哦!');}}return function(){compute()}})();window.onload = function () {init();}function init() {initCompute(77);}
// 课时十二作业// 1、打印一个参数值能够同时被3或者是5或者是7同时整除的数// 2、打印斐波拉契数列// 3、打印从0到一个数的累加值// 使用模块化开发window.onload = function(){init();}function init(){console.log(initDiv(10));console.log(initFb(12));console.log(initAdd(100));isLeapYear();}// 打印一个参数值能够同时被3或者是5或者是7同时整除的数var initDiv = (function(){var arr = [];return function(val){for(var i = 0; i < val; i++){if(i % 3 === 0 && i % 5 === 0 || i % 7 === 0){arr.push(i);}}return arr;}})();// 打印斐波拉契数列var initFb = (function(){function fb(num){var num = num || 0;if(num < 0){return 0;}if(num <= 2){return 1;}return fb(num - 1) + fb(num - 2);}return fb;})();// 打印从0到一个数的累加值var initAdd = (function(){return function add(num){var num = num || 0;if(num === 1){return 1;}return num + add(num -1);}})();// 从window.prompt输入一个年份判断是不是闰年,用三目运算符var isLeapYear = (function(){return function(){var year = Number(window.prompt('请输入年份'));// if(year % 4 === 0 && year % 100 !== 0 || year % 400 === 0){// console.log('是闰年');// }else{// console.log('是平年');// }return year = year % 4 === 0 && year % 100 !== 0 || year % 400 === 0 ? console.log('是闰年'): console.log('是平年')}})();
