js 面向对象中,定义一个函数的过程

定义一个函数做的两件事:
1: 实例化一个Function对象;
2: 实例化一个Object对象,并给该函数扩展prototype属性指向这个构造函数

 

大致过程如图所示:

 

每一种引用类型(函数,对象,数组)都有__proto__属性,并且其__proto__属性指向其构造模板的prototype(原型对象)。

函数比较特殊,定义一个函数,分为上述两个步骤。

 

 

验证:

<!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 Student(n,a){
            this.name = n;
            this.age = a;
        }
        //原型对象中加入方法
        Student.prototype.sayHi = function(){
            console.log("hello");
        }
        //实例化对象
        var stu = new Student("xiaoming",20);
    </script>
</body>
</html>

posted @ 2019-12-11 10:35  qy_sk  阅读(1592)  评论(1)    收藏  举报