构造函数和继承

184 阅读1分钟

es6之前(es5)是 不用类 创建对象的 通过构造函数

类的创建有三种方式:

1.字面量创建

2.new Object

3.构造函数

实例成员:

不用开辟新的内存空间

原型里面有一个constructor 作用就是 说明当前的对象是哪个构造函数创建出来的 很多情况下 我们需要手动的利用constructor 这个属性指回 原来的构造函数

不是很明白我们看看下面的代码:

__proto__叫做对象的原型

查找机制:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    function Person(uname ,age) {
        this.uname = uname;
        this.age = age;
        // this.sing = function(){
        //     console.log('我会做饭');
        // }    
    }
   
//    Object.prototype.sex = '他就是一个不会事件管理的男神,如今都无人问津!!!'//3
// Person.prototype.sex = '他是一个情窦初开的男生,而且长得很man' //2

    var lumingqing = new Person('小酷','22');
//    lumingqing.sex = '他是一个拾金不昧的孩子,而且长得很帅' //1.先去自身查找
   console.log(lumingqing.sex);//4.undefined
   
    
    
</script>
</html>

一层一层往上爬

扩展内置对象

实现不了