zoukankan      html  css  js  c++  java
  • JavaScript原型及原型链

    代码一:

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
        <script>
            function Car (desc) {
                this.desc = desc;
                this.color = "red";
            }
    
    //        Car.prototype = {
            //            getInfo: function() {
            //                return 'A ' + this.color + ' ' + this.desc + '.';
            //            }
            //        };
            //instantiate object using the constructor function
            var car=new Car("bmw");
    //        var car =  Object.create(Car.prototype);
    //        console.log(car.getInfo());
            console.log(car instanceof  Car);
            console.log(car.color);
            console.log(car.desc);
        </script>
    </head>
    <body>
    
    </body>
    </html>


    以上code输出结果为:  

    true
    red
    bmw

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
        <script>
            function Car (desc) {
                this.desc = desc;
                this.color = "red";
            }
    
    //        Car.prototype = {
            //            getInfo: function() {
            //                return 'A ' + this.color + ' ' + this.desc + '.';
            //            }
            //        };
            //instantiate object using the constructor function
    //        var car=new Car("bmw");
            var car =  Object.create(Car.prototype);
    //        console.log(car.getInfo());
            console.log(car instanceof  Car);
            console.log(car.color);
            console.log(car.desc);
        </script>
    </head>
    <body>
    
    </body>
    </html>
    

     以上code输出结果:

    true

    undefined

    undefined


    问题来了:

    通过new+构造函数构建实例,实例能访问构造函数的属性

    通过Object.create(prototype)构建实例时,实例对象不能访问构造函数的属性

    以上结果出现的理论依据在哪里。找出来

  • 相关阅读:
    (转)了解JNDI
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
    占个座
    关于 inode 与 iblock 的知识
    机器学习性能度量指标:ROC曲线、查准率、查全率、F1
    编译出现的问题解决
    二分查找(Binary Search)
    ST算法 Sliding Window algorithm template
    数据结构_算法
    知识点积累
  • 原文地址:https://www.cnblogs.com/web-coding/p/4720776.html
Copyright © 2011-2022 走看看