原型 —> 继承

function Teacher(){this.name = 'Mr.Li';this.tSkill = 'JAVA';}Teacher.prototype = {pSkill: 'JS'}var t = new Teacher();console.log(t);function Student(){this.name = 'Mr.Wang';}// 中间件-缓冲function Buffer(){}Buffer.prototype = Teacher.prototype; // 继承了Teacher的原型var buffer = new Buffer();Student.prototype = buffer;Student.prototype.age = 18; // 修改Student原型不影响Teacher的原型var s = new Student();console.log(s);// 继承和隔离修改Student原型不影响Teacher的原型// 圣杯模式function Teacher(){}function Student(){}function Buffer(){}//Buffer.prototype = Teacher.prototype;//var buffer = new Buffer();//Student.prototype = buffer;inherit(Student, Teacher);var s = new Student();var t = new Teacher();封装function inherit(Target, Origin){function Buffer(){}Buffer.prototype = Origin.prototype;Target.prototype = new Buffer();Target.prototype.constructor = Target; // 将构造器指回本身 还原构造器Target.prototype.super_class = Origin; // 继承源}
圣杯布局模式
闭包
- 函数形式

- 对象形式

构造函数形成闭包 ```typescript function Compute(){ var num = 0;
this.add = function(){ num++; console.log(num); }
this.minus = function(){ num—; console.log(num); } // var compute = {} 对象形式 return compute // vat this = {} new生成this,然后隐式return this // return this; // reutrn 123;无效,忽略 return {} 引用值就不会return this }
var compute = new Compute(); compute.add();
4. 圣杯模式-闭包```typescriptvar inherit = (function(){var Buffer = function(){} //Buffer内部属性,不会污染全局return function inherit(Target, Origin){Buffer.prototype = Ofigin.prototype;Target.prototype = new Buffer();Target.prototype.constructor = Target; // 将构造器指回本身 还原构造器Target.prototype.super_class = Origin; // 继承源}})(); // 自执行inherit(Student,Teacher);// 防止变量全局污染// 模块化开发
封装
var initCompute = (function(){var a=1,b=2;function add(){console.log(a+b);}function minus(){console.log(a-b);}function mul(){console.log(a*b);}function div(){console.log(a/b);}return function(){add();minus();mul();div();}})();var initFunctions = (function(){})();function init(){initCompute();initFunctions();}window.onload = function(){init();}
插件化// 自启动;(function(){var Slider = function(opt){}Slider.prototype = {}window.Slider = Slider;})();var slider = new Slider({})// 手动执行var Test = (function(){var Test = function(){}Test.prototype={}return Test;})();

立即执行函数执行后返回fb保存在initFb(全局变量)中,然后该执行函数销毁,调用initFb即调用fb
