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>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM