面向对象-原型

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
     function Fn(n) {
         this.name = n;
         this.show = function(){
             console.log(this.name)
         }
     }
     var f1 = new Fn("admin");
     var f2 = new Fn("root");
     console.log(f1 == f2)    //f
     f1.show();
     f2.show();
     console.log(f1.show == f2.show)   //f



     console.log(Fn)
     console.dir(Fn)
     console.dir(Fn.prototype)
     console.dir(Fn.__proto__)

    // prototype:函数的原型属性(对象):做什么的?将来被new出来的对象的父级
        // constructor:表示当前对象所属的函数,表示自己属于哪个函数

    // __proto__:数据的原型链,js中所有数据,都有这个属性,表示:自己的父级
    
     Fn.qwe = function(){};
     Fn.prototype.qwe = function(){};

     console.log(f1)
     console.log(f2)
     console.log(f1.__proto__.qwe)
     console.log(f1.__proto__ === Fn.prototype)
     console.log(f2.__proto__ === Fn)
    
    // 函数的父级(来源):Function()
    // f1的父级(来源):Fn.prototype
    // f2的父级(来源):Fn.prototype

    // String()
    // Number()
    // Boolean()
    // Array()
    // Object()
    // Function()

    // =====================================


     function Fun(n){
         this.name = n;
     }

     Fun.prototype.show = function(){
         console.log(this.name)
     }

     console.log(Fun.prototype)

     var f1 = new Fun("admin");
     var f2 = new Fun("root");

     console.log(f1 === f2)

     console.log(f1.show === f2.show);//true
     console.log(f1.__proto__ === f2.__proto__);//true
     console.log(f1.__proto__.show === f2.__proto__.show);//true

     f1.__proto__.show();
     f2.__proto__.show();

     console.log(f1)
     console.log(f2)

     f1.show();
     f2.show();

     console.log(f1.show == f2.show)

    // 当对象访问自身属性或方法时,如果自身有,直接使用
    //                         如果自身没有,会顺着__proto__找自己的父级身上是否存在,有,就使用,没有,继续向上找,到顶之后,还没有,就抛出undefined


// ============================

     var arr1 = new Array(3,4,5)
     var arr2 = new Array(3,4,5)
     console.log(arr1 == arr2)
     console.log(arr1)
     console.log(arr2)

     // arr1.push
     // arr2.push

     console.log(arr1.push === arr2.push)//true

// ==================================

    // 面向对象的过程中:
    //     将属性写在构造函数内
    //     将方法写在构造函数的原型上


    // 实例:实际的案例
    // 构造函数(类)是实例的抽象化
    // 实例是构造函数(类)的具象化
</script>
</html>
复制代码

 

posted @   菜鸟小何  阅读(195)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示