js高级知识点

1.对构造函数的优化

        第一种:对构造函数优化的方法

<script>
    function study(){
            console.log("我爱学习");
        }
        function Eat() {
            console.log("吃饭");
        }
        //第一种:对构造函数进行优化
        function Student(stuid, stuname) {
            this.stuid = stuid
            this.stuname = stuname
            this.study = study
            this.Eat = Eat
        }
        //实例化一个对象,使用new来实例化
        var stu_1 = new Student("1001", "张三")
        stu_1.study()
        stu_1.Eat() 
</script>

        第二种:对构造函数优化的方法

<script>
    var fns = {
            study: function () {
                console.log("我爱学习");
            },
            Eat: function () {
                console.log("吃饭");
            }
        }
        //第二种:对构造函数进行优化
        function Student(stuid, stuname) {
            this.stuid = stuid
            this.stuname = stuname
            this.study = fns.study
            this.Eat = fns.Eat
        }
        //实例化一个对象,使用new来实例化
        var stu_1 = new Student("1001", "张三")
        stu_1.study()
        stu_1.Eat()
</script>

2.原型prototype

        使用原型时,一般情况下,对象的属性不会放在原型上创建,会直接放在构造函数中,除非这个属性的值是共享的数据,此时就可以放在原型上;一般需要共享的函数也可以放在原型上,这样的话,每个实例化的对象中都包含了此函数 

                构造函数、原型对象和实例化对象三者之间的关系

                构造函数创建时会自动添加一个prototype原型对象,prototype又可以通过constructor来重新指向该构造函数

                构造函数可以通过new实例化一个对象,此时会添加一个隐式原型_proto_,那么该对象就可以通过对象._proto_.constructor重新指向该构造函数

              实例化对象可以通过隐式原型__proto_指向prototype原型对象

console.log(Student.prototype);
        Student.prototype.age = 20 //给原型prototype中创建一个属性age
        Student.prototype.running = function () {//给原型prototype中创建一个属性running
            console.log("跑步");
        }
        console.log(stu_1.age);
        console.log(Student.prototype);
        stu_1.running()

        console.log(stu_1.age);
        console.log(Student.prototype);
        // 通过Student的原型对象中的constructor,可以重新指向Student
        console.log(Student.prototype.constructor === Student);
        //通过实例化对象时会创建一个隐式原型__proto__,它指向的就是Student.prototype
        console.log(stu_1.__proto__ === Student.prototype);
        //通过实例化对象的隐式原型__proto__中的constructor就可以指向Student
        console.log(stu_1.__proto__.constructor === Student);

3.原型链

        访问一个对象的属性时

        先在自身属性中查找,找到返回

        如果没有,再沿着_proto_这条链上查找,找到返回

        如果最终没有找到,返回undefined

        别名:隐式原型链

        作用:查找对象的属性(方法)

                stu_1.age=30

                console.log(stu_1.age);

                hasOwnProperty:检查对象本身是否包含某个属性,

                如果有则返回true,反之则返回false

                console.log(stu_1.hasOwnProperty("age")); //false

                console.log(stu_1.__proto__.hasOwnProperty("age"));  //true

                in:检查对象中是否包含某个属性,如果对象本身没有,

                但原型对象上有,也可以返回true

                console.log("age" in stu_1);   //true */

// 练习 原型链
        //现由一个构造函数Student
        function Person(name,sex){
            this.name = name
            this.sex = sex
        }
        var p0 =new Person("zhangsan","男")
        // p0.age = 20
        // Person.prototype.age = 30
        console.log(p0.age); //打印出20,打印出30,打印出undefined
        console.log(p0.__proto__ === Person.prototype); //打印为true
        console.log(p0.__proto__.constructor == Person); //打印为true
        console.log(Person.prototype.constructor ==Person); //打印为true
        console.log(Person.prototype.__proto__ === Object.prototype); //打印为true
        console.log(Object.prototype.__proto__); //打印为null

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值