清晰了解原型链

16 篇文章 0 订阅
12 篇文章 0 订阅

看完这段代码:

<!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>
    <script>
        /* 核心:原型对象和原型链 */
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }
        Person.prototype.name = "名字";
        Person.prototype.age = 99;
        Person.prototype.showName = function() {
            console.log("name", this.name);
        }
        Person.prototype.showAge = function() {
            console.log("name", this.name);
        }
        Person.show = function() {
            console.log("show", this.name);
        }
        var p1 = new Person("zs", 19);

        p1.show(); //show zs ? => 报错
        Person.prototype.show(); //报错
        Person.show(); //show Person

        /* 概念 */
        /* 构造函数:Person */
        /* 原型对象: Person.prototype */
        /* 实例对象:p1 */
        // console.log(p1.name); //zs
        // console.log(Person.prototype.name); //名字
        // console.log(p1.age); //19
        // console.log(Person.prototype.age); //99
        // console.log(Person.name); //zs ? 名字? => Person 函数是特殊的对象,函数有一个name属性
        /* 对象在访问属性和方法的时候,先在自己身上查找,如果有那么就直接用,没有就找以下原型对象 */

        p1.showName(); //name zs
        Person.prototype.showName(); //name 名字 
        p1.__proto__.showName(); //name 名字
        Person.showName(); //报错    Person自己并没有showName方法,所以在这里会报错
    </script>
    <script>
        function Person(name, age) {
            var className = "2020";

            function showClassName() {
                console.log(className);
            }
            this.name = name;
            this.age = age;
            this.showInfo = function() {}
            this.infoShow = function() {
                return className;
            }
        }
        Person.tt = "我是tt";
        Person.prototype.name = "名字";
        Person.prototype.age = 99;
        Person.prototype.showName = function() {
            console.log("name", this.name);
        }
        Person.prototype.showAge = function() {
            console.log("name", this.name);
        }
        Person.show = function() {
            console.log("show", this.name);
        }
        var p1 = new Person("zs", 19);
        // console.log(className, p1.className); //报错,undefined 
        // showClassName(); //Uncaught ReferenceError: showClassName is not defined
        console.log(p1.infoShow());

        /* 构造函数:Person */
        /* 原型对象: Person.prototype */
        /* 实例对象:p1 */
        /* 成员:对象的属性和方法叫做成员 */
        // 原型成员:原型对象的属性(Person.prototype.name\Person.prototype.age)和方法(Person.prototype.showName\Person.prototype.showAge)。
        /* 实例成员:实例对象的属性( this.name \ this.age)和方法(this.showInfo)。 */
        /* 静态成员:构造函数自己的属性(Person.tt)和方法(Person.show)。 */
        /* 私有成员:构造函数内部声明的私有变量(className)和私有函数(showClassName函数)。 */
        /* 特权方法:特殊的实例方法(该方法可以访问到私有成员)。 */
        /* 实例化:构造函数创建实例对象的过程,我们叫做实例化。 */
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值