javascript 原型链

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript">
        /*function Person(name){
            this.name = name
        }*/
        var Person = function(name){
            this.name = name;
        };
        Person.prototype.getName = function(){
            return this.name;
        };
        var oPerson01 = new Person('fengyouqi');
        oPerson01.getName();
        /*
        * JS在创建对象(不论是普通对象还是函数对象)的时候,都有一个叫做 _proto_ 的内置属性,用于指向创建它的函数对象的原型对象prototype
        * console.log(oPerson01.__proto__ === Person.prototype)     //true
        * */
        console.log(oPerson01.__proto__ === Person.prototype);      //true
        /*
        * 同样,Person.prototype对象也有 _proto_ 属性,它指向创建它的函数对象(Object)的prototype
        * console.log(Person.prototype.__proto__ === Object.prototype)   //true
        * */
        console.log(Person.prototype.__proto__ === Object.prototype);   //true
        /*
        * 继续,Object.prototype对象也有 _proto_ 属性,但它比较特殊,为null
        * console.log(Object.prototype.__proto__)     //null
        * */
        console.log(Object.prototype.__proto__);     //null
        /*
        *   这个有 _proto_ 串起来的直到Object.prototype._proto_为null的链叫做原型链
        * */
       </script>
</head>
<body>

</body>
</html>

参考:

http://blog.csdn.net/i10630226/article/details/48689561

http://www.cnblogs.com/dolphinX/p/4385862.html

http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javascript.html

posted @ 2017-11-05 20:41  我将枕中记忆抹去任岁月浮光掠影  阅读(100)  评论(0编辑  收藏  举报