function Person(name) { this.name = name; } Person.prototype.constructor === Person // true Person.prototype = { method: function () {} }; Person.prototype.constructor === Person // false Person.prototype.constructor === Object // true
function Sub(value) { Super.call(this); this.prop = value; } // 上面代码中,Sub是子类的构造函数,this是子类的实例。在子类实例上调用父类的构造函数Super,就会让子类实例具有父类实例的属性。
2. 第二步,是让子类的原型指向父类的原型,这样子类就可以继承父类原型。
Sub.prototype = Object.create(Super.prototype); Sub.prototype.constructor = Sub; Sub.prototype.method = ‘...‘; // 举例来说,下面是一个Shape构造函数。 function Shape() { this.x = 0; this.y = 0; } Shape.prototype.move = function (x, y) { this.x += x; this.y += y; console.info(‘Shape moved.‘); }; // 我们需要让Rectangle构造函数继承Shape。 // 第一步,子类继承父类的实例 function Rectangle() { Shape.call(this); // 调用父类构造函数 } // 另一种写法 function Rectangle() { this.base = Shape; this.base(); } // 第二步,子类继承父类的原型 Rectangle.prototype = Object.create(Shape.prototype); Rectangle.prototype.constructor = Rectangle;
// 有时只需要单个方法的继承,这时可以采用下面的写法。 ClassB.prototype.print = function() { ClassA.prototype.print.call(this); // some code } // 上面代码中,子类B的print方法先调用父类A的print方法,再部署自己的代码。这就等于继承了父类A的print方法。
function M1() { this.hello = ‘hello‘; } function M2() { this.world = ‘world‘; } function S() { M1.call(this); M2.call(this); } // 继承 M1 S.prototype = Object.create(M1.prototype); // 继承链上加入 M2 Object.assign(S.prototype, M2.prototype); // 指定构造函数 S.prototype.constructor = S; var s = new S(); s.hello // ‘hello‘ s.world // ‘world‘ // 上面代码中,子类S同时继承了父类M1和M2。这种模式又称为 Mixin(混入)。
var module1 = new Object({ _count : 0, m1 : function (){ //... }, m2 : function (){ //... } }); module1.m1(); module1._count = 5;
function StringBuilder() { var buffer = []; this.add = function (str) { buffer.push(str); }; this.toString = function () { return buffer.join(‘‘); }; }
// 使用“立即执行函数”(Immediately-Invoked Function Expression,IIFE),将相关的属性和方法封装在一个函数作用域里面,可以达到不暴露私有成员的目的。 var module1 = (function () { var _count = 0; var m1 = function () { //... }; var m2 = function () { //... }; return { m1 : m1, m2 : m2 }; })(); // 使用上面的写法,外部代码无法读取内部的_count变量。 console.info(module1._count); //undefined // 上面的module1就是 JavaScript 模块的基本写法。
var module1 = (function (mod){ mod.m3 = function () { //... }; return mod; })(module1); // 上面的代码为module1模块添加了一个新方法m3(),然后返回新的module1模块。
var module1 = (function (mod) { //... return mod; })(window.module1 || {}); // 与"放大模式"相比,“宽放大模式”就是“立即执行函数”的参数可以是空对象。
2. 输入全局变量
var module1 = (function ($, YAHOO) { //... })(jQuery, YAHOO); // 上面的module1模块需要使用 jQuery 库和 YUI 库,就把这两个库(其实是两个模块)当作参数输入module1。这样做除了保证模块的独立性,还使得模块之间的依赖关系变得明显。
3. 命名空间的作用
(function($, window, document) { function go(num) { } function handleEvents() { } function initialize() { } function dieCarouselDie() { } //attach to the global scope window.finalCarousel = { init : initialize, destroy : dieCarouselDie } })( jQuery, window, document ); // 上面代码中,finalCarousel对象输出到全局,对外暴露init和destroy接口,内部方法go、handleEvents、initialize、dieCarouselDie都是外部无法调用的。
原文:https://www.cnblogs.com/wz-front-end/p/14966017.html