zoukankan      html  css  js  c++  java
  • 【原创】关于对象+原型+继承(三)

    先看几个例子:

     1 function a(name,color){  
     2     this.name=name;  
     3     this.color=color;  
     4     this.geta=function() {    
     5       return('this is a'); 
     6     }
     7 }
     8 a.prototype.price=-100;
     9 a.prototype.rating=3;
    10 a.prototype.getb=function(){
    11     return ("this is another a");
    12 };
    
    15 var tt=new a("ton","red");

     1 tt; //输出:a { name="ton", color="red", price=-100, 更多...} //此处tt输出的更多包括如下图片

     有点绕,理清了就简单多了

     1 a;//输出a(name, color)
     2 
     3 a.prototype;//输出a { price=-100,  rating=3,  getb=function()}
     4 
     5 a.__proto__;//输出function() ,小写的f
     6 
     7 a.constructor;//输出Function(),大写的F
    8 a.constructor.prototype;//输出function() 9 10 a.constructor.prototype===a.__proto__;//输出true 11 12 a.prototype.constructor===a;//输出true 13 14 a.prototype.constructor;//输出a(name, color)
     1 tt.prototype;//输出undefined
     2 
     3 tt.__proto__;//输出a { price=-100,  rating=3,  getb=function()}
     4 
     5 tt.__proto__===a.prototype;//输出true
     6 
     7 tt.constructor;//输出a(name, color)
     8 
     9 tt.constructor.prototype;
    10 //输出a { price=-100,  rating=3,  getb=function()}
    11 
    12 tt.prototype.constructor;//报错TypeError: tt.prototype is undefined

    三者间的关系如下:

    1 tt.constructor===a;//true
    2 
    3 tt.__proto__===a.prototype;//true
    4 
    5 a.prototype.constructor===a;//true
    6 
    7 a.prototype.constructor.prototype===a.prototype;//true

    看下图

    比较懒,随手画了张符,给自己看的,见谅见谅~~~~

    a.prototype=b;//设置原型为b;

    构造器函数a()的原型属性设置为指向b;

    构造器函数新建了一个叫做tt的实例化对象

    并理解为:原型 b 是实例化对象tt的原型

    【还是有点乱,有无高人指点一二】

    最新了解请见:《javascript面向对象编程指南》object-oriented javascript,里面第五章:原型和第六章:继承讲得比博主的涂鸦好多了

  • 相关阅读:
    第十六章 课程总复习
    第四章 数据类型及数据库基本操作
    第二章.图形化管理工具
    第十三章 指导学习:人机猜拳
    洛谷 P4396 (离散化+莫队+树状数组)
    洛谷 P1351 (枚举)
    洛谷P5322 (BJOI 2019) DP
    P3376 网络最大流模板(Dinic + dfs多路增广优化 + 炸点优化 + 当前弧优化)
    洛谷 P2176(最短路)
    HDU 6556 (2018CCPC吉林 B题)
  • 原文地址:https://www.cnblogs.com/pm-dongjian/p/5022306.html
Copyright © 2011-2022 走看看