zoukankan      html  css  js  c++  java
  • 语法——原型prototype深入理解

    console.log('-----------------------------Object');
    
    var obj = {};
    console.log(obj.__proto__ === Object.prototype);                // true
    console.log(Object.prototype);                                  // Object
    console.log(obj.prototype);                                     // undefined
    
    console.log('-----------------------------Function');
    
    // prototype is a Object property of a Function
    var func = function () {};
    console.log(func.__proto__ === Function.prototype);             // true
    console.log(func.prototype === Function.prototype);             // false
    console.log(func.prototype === Object.prototype);               // false
    
    console.log(func.__proto__.__proto__ === Object.prototype);     // true
    console.log(func.prototype.__proto__ === Object.prototype);     // true
    
    console.log('-----------------------------new func');
    
    // we look for say from this, then from __proto__ up
    func.say = function () { console.log('func say'); }
    func.__proto__.say = function () { console.log('func proto say'); }
    func.prototype.say = function () { console.log('func prototype say'); }
    
    // From this, we know that __proto__ is only used as pointer to find parent proto.
    // And what the newed object has is determined by its prototype, and prototype.__proto__ etc.
    //func.prototype.__proto__ = func.__proto__;
    
    var new_func = new func();
    new_func.say();
    func.say();
    
    // var new_func = {};
    // new_func.__proto__ = func.prototype;
    // func.call(new_func);
    
    console.log(new_func.__proto__ === func.prototype);             // true
    console.log(new_func.prototype);                                // undefined, as it's an Object
    
    func.prototype.say = function () { console.log('func prototype say 2'); }
    new_func.say(); // say 2
    
    console.log('-----------------------------derived');
    
    var de_func = function () {
    
    };
    de_func.prototype.__proto__ = func.prototype;
    
    var new_de = new de_func();
    new_de.say();
    
    // remember:
    // 1. var x = new obj(); means x.__proto__ = obj.prototype
    // 2. we look for member from this first, then from this.__proto__

  • 相关阅读:
    hdu 5532 Almost Sorted Array(模拟)
    hdu 2612 Find a way(bfs)
    hdu 2660 Accepted Necklace(dfs)
    reactjs学习一(环境搭配react+es6+webpack热部署)
    途牛banner自动轮播
    web app开发技巧总结
    20个为前端开发者准备的文档和指南
    Github上最受关注的前端大牛 快来膜拜把!
    怎么才能成为优秀的前端开发工程师?
    Web前端知识技能大汇总
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7387777.html
Copyright © 2011-2022 走看看