zoukankan      html  css  js  c++  java
  • 一幅图看懂prototype与[[Prototype]]

    首先明确:

    1、任何对象都有属性[[Prototype]];

    2、只有函数有属性prototype。

    Pet为父类,子类Dog继承Pet。示意图如下:

    继承的样例代码:

        // 父类构造函数
        function Pet(name,sound){
            var name = name;
            this.sound = "Pet says " + sound;
            this.getName = function(){
                console.log(name);
            };
        }
        // 父类原型
        Pet.prototype.voice = function(){
            console.log(this.sound);
        }
    
        // 子类
        function Dog(sound){            
            this.sound = "Dog syas " + sound;
        }
    
        // 继承
        Dog.prototype = new Pet("pet","ohooo");
        Dog.prototype.constructor = Dog;
    
        // 继承验证
        var dog = new Dog("wangwang");
        dog.voice();//Dog syas wangwang
        dog.getName();//pet
    Firefox中的显示如下:

  • 相关阅读:
    8.5
    8.12
    8.11
    8.14
    8.15
    8.18
    8.16
    8.20
    Android新版NDK环境配置(免Cygwin)
    在Windows7上搭建Cocos2d-x win32开发环境
  • 原文地址:https://www.cnblogs.com/dhuhank/p/4457484.html
Copyright © 2011-2022 走看看