ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

关于原型JavaScript

2022-08-05 14:33:55  阅读:483  来源: 互联网

标签:__ ldh Star log JavaScript 原型 关于 console prototype


原型:

<script>
        // 构造函数
        function Star(uname, age){
            this.uname = uname;
            this.age = age;
            this.dance = function(){
                console.log("跳舞");
            } 
        }

        Star.prototype.sing = function(){
            console.log('我会唱歌');
        }

        var ldh = new Star('刘德华', 18);
        var zxy = new Star('张学友', 19);

        console.log(ldh.dance === zxy.dance);
        // false

        console.log(ldh.sing === zxy.sing);
        // true
        // 定义是放在构造函数里面,构造方法在属性里面比较占据内存,一般把原型方法放在原型对象上面,

    </script>

 

对象原型

<script>
        // Star的构造方法
        // prototype是为函数添加方法
        function Star(uname, age) {
            this.uname = uname;
            this.age = age;
        }
        Star.prototype.sing = function() {
            console.log('我会唱歌');
        }
        var ldh = new Star('刘德华', 18);
        var zxy = new Star('张学友', 19);
        // 属性的__proto__ 指向的是方法原型链上的方法
        ldh.sing();
        console.log(ldh); // 对象身上系统自己添加一个 __proto__ 指向我们构造函数的原型对象 prototype
        console.log(ldh.__proto__ === Star.prototype);
        // 方法的查找规则: 首先先看ldh 对象身上是否有 sing 方法,如果有就执行这个对象上的sing
        // 如果么有sing 这个方法,因为有__proto__ 的存在,就去构造函数原型对象prototype身上去查找sing这个方法

        // 也就是说Star构造函数的prototype 和 对象上的 ldh 的__proto__ 指向是同一个位置
    </script>

 

原型constructor

<script>
        function Star(uname, age){
            this.uname = uname;
            this.age = age;
        }

        Star.prototype = {
            // 可以利用 Star.prototype = {} 修改constructor属性,但必须把constructor指向方法
            constructor: Star,
            sing: function () {
                console.log('我会唱歌');
            },
            movie: function () {
                console.log('我会看电影');
            },
        }

        var ldh = new Star('刘德华', 18)

        console.log(Star);
        console.log(Star.prototype);

        // constructor相当于查看函数的初始构造函数
        console.log(Star.prototype.constructor);
        console.log(ldh.__proto__.constructor);

    </script>

 

原型链

<script>
        function Star(uname, age) {
            this.uname = uname;
            this.age = age;
        }
        Star.prototype.sing = function() {
            console.log('我会唱歌');
        }
        var ldh = new Star('刘德华', 18);

        console.log(typeof ldh);

        // 1. 只要是对象就有__proto__ 原型, 指向原型对象
        console.log(Star.prototype);
        console.log(ldh.__proto__);

        // 首先 Star.prototype,是找Star的原型链以及里面的方法
        // ldh.__proto__ 是对象ldh的原型链,由于新建一个对象出来,原型链也是Star.
        // js万物皆对象,所以Star也是继承Object的对象.所以Star.prototype.__proto__的原型是Object
        // 最终Object指向null 

        console.log(Star.prototype.__proto__ );
        console.log(Star.prototype.__proto__ === Object.prototype);
        // 2.我们Star原型对象里面的__proto__原型指向的是 Object.prototype
        console.log(Object.prototype.__proto__);
        // 3. 我们Object.prototype原型对象里面的__proto__原型  指向为 null
    </script>

 

标签:__,ldh,Star,log,JavaScript,原型,关于,console,prototype
来源: https://www.cnblogs.com/coderatian/p/16554216.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有