V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
temberature
V2EX  ›  前端开发

[原创]更自然的理解 JS 中的构造函数&原型链

  •  
  •   temberature · 2018-05-11 10:19:55 +08:00 · 1592 次点击
    这是一个创建于 2169 天前的主题,其中的信息可能已经有所发展或是发生改变。

    格式更好看的在 Github,https://github.com/temberature/blog/issues/10

    首先最简单的:

    var o = new Object();
    
    Object.prototype === o.__proto__; //true
    o.constructor === Object; //true
    o.hasOwnProperty('constructor'); //false
    

    构造函数和原型对象之间是相互指向的。 因为没办法修改 Object 构造函数,所以只能通过字面量完成初始化过程。

    var o = {
    	a: 0
    }
    

    如果是初始化参数是变量呢?

    var a = b ? 0 : 1;
    var o = {
    	a: a
    }
    

    封装一下

    function O (b) {
    	var a = b ? 0 : 1;
    	return {
    		a: a
    	}
    }
    var o = O(1); // var o =new O(1);
    
    o.constructor === O; //false
    o instanceof O; //false
    o.__proto__ === O.prototype; //false
    

    这又被称为工厂模式。 如果出现了局部变量,

    function O (b) {
    	var c = 2;
    	var a = (b + c) ? 0 : 1;
    	return {
    		a: a,
    		sayC: function () {
    			alert(c);
    		}
    	}
    }
    var o = O(1);
    

    这又被称为稳妥构造函数。 如果是自定义的构造函数又是怎么样的呢?

    function F () {}
    var f = new F();
    
    F.prototype === f.__proto__; //true
    f.constructor === F; //true
    f.hasOwnProperty('constructor'); //false
    F.prototype.constructor === F; //true
    f instanceof F; //true
    

    如果换种写法

    function F () {}
    F.prototype = new Object();
    var f = new F();
    
    F.prototype === f.__proto__; //true
    f.constructor === F; //false
    f.hasOwnProperty('constructor'); //false
    F.prototype.constructor === F; //false
    f instanceof F; //true
    

    虽然 instanceof 操作符结果没问题(就是判断两者的原型指向是否相等),但是原型对象没有指向构造函数。

    function F () {}
    F.prototype = new Object();
    F.prototype.constructor = F;
    var f = new F();
    
    F.prototype === f.__proto__; //true
    f.constructor === F; //true
    f.hasOwnProperty('constructor'); //false
    F.prototype.constructor === F; //true
    f instanceof F; //true
    

    这被称为组合模式 如果把原型定义放在构造函数内

    function F () {
    	if (this.constructor !== F) {
    		F.prototype = {
    			constructor: F
    		}
    	}
    }
    

    这又叫做动态原型。 如果把原型对象换成变量,再封装一下

    function create (o) {
    	function F () {}
    	F.prototype = o;
    	return new F();
    }
    var f = create(new Object());
    
    f.constructor === Object; //true
    f.hasOwnProperty('constructor'); //false
    f instanceof Object; //true
    

    为什么没有改写 constructor,因为 F 在外面起不了作用的,如果要判断只能根据传入对象的构造函数判断,这也是为什么后面会进一步称其为寄生式继承,因为没有独立的自定义类型。 这也就是 ES5 中的 Object.create, 只到这里没有意义,还要有增强的功能。

    var f = Object.create(new Object());
    f.localFn = function () {}
    

    封装一下

    function createAnother (o) {
    	var f = Object.create(o);
    	f.localFn = function () {};
    	return f;
    }
    

    反过头看下更精细的控制,共享的东西放在原型对象上,本地内容放在构造函数里。 这就叫组合继承。

    function F () {
    	this.localVal = 0;
    	this.localFn = function () {}
    }
    F.prototype = {
    	constructor: F,
    	shareVal: 1,
    	shareFn: function () {}
    }
    

    如果要从 F 继承下去呢?

    function S () {
    	F.call(this);
    }
    S.prototype = new F();
    S.prototype.constructor = S;
    

    为了避免两次调用 F 的问题,我们尝试直接基于 F.prototype。

    function S () {
    	F.call(this);
    }
    S.prototype = F.prototype;
    S.prototype.constructor = S;
    var s = new S();
    s.constructor === S; //true
    s instanceof S; //true
    S.prototype.__proto__.constructor === F; //false
    

    问题在于 S 和 F 原型相同,破坏了继承的层级结构。

    function S () {
    	F.call(this);
    }
    S.prototype = Object.create(F.prototype);
    S.prototype.constructor = S;
    var s = new S();
    s.constructor === S; //true
    s instanceof S; //true
    S.prototype.__proto__.constructor === F; //true
    

    这里实际借助了替换构造函数,避免实际构造函数的两次执行。这就是寄生组合式继承。

    第 1 条附言  ·  2018-05-11 10:50:16 +08:00
    参考:《 JavaScript 高级程序设计》第 3 版第 6 章
    5 条回复    2018-05-11 10:43:37 +08:00
    ck65
        1
    ck65  
       2018-05-11 10:23:46 +08:00 via iPhone
    「更自然地理解……」
    kisnows
        2
    kisnows  
       2018-05-11 10:27:26 +08:00
    最近面试,问候选人 js 的原型链是什么,候选人全都往构造函数上去扯,很郁闷。
    temberature
        3
    temberature  
    OP
       2018-05-11 10:31:33 +08:00
    @kisnows 我也是这几天面试,突然明白了各种继承的内在逻辑关系
    SourceMan
        4
    SourceMan  
       2018-05-11 10:32:18 +08:00
    看不懂
    temberature
        5
    temberature  
    OP
       2018-05-11 10:43:37 +08:00
    @SourceMan 我写的也比较粗糙,不过提供了一种与《高程》不同的展开思路
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5310 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:07 · PVG 17:07 · LAX 02:07 · JFK 05:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.