码迷,mamicode.com
首页 > Web开发 > 详细

js模拟类的创建以及继承

时间:2017-11-26 20:34:12      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:stun   eof   添加   create   html   str   cto   继承   var   

<html>

<body>

    <script>
    //js模拟类的创建以及继承
    
    //第一步:创建父类
        function Parent(name){
            this.name = name;
        }
        //给父类添加属性方法
        Parent.prototype.age = 18;
        //var p1 = new Parent();
    //第二步:创建子类    
        function Child(){
            Parent.call(this,"asdfasfd");
            
        }
        
        //第三步:确定继承的关系
        
        Child.prototype = Object.create(Parent.prototype);
        
        Child.prototype.stuno = "2000";
        
        //第四步:改造构造器(不是很重要)
        //改变了某个构造器的原型之后,紧接着的代码一定是改构造器
        Child.prototype.constructor = Child;
        /* Object.create的实现
        function create(proto){
            function F(){
            
            
            }
            F.prototype = proto;
            var temp = new F();
            return temp;
        }
        
        */
        //var o = new Parent();
        //o instanceof Object;
        
        
        //constructor
        
        
    </script>
</body>

</html>

js模拟类的创建以及继承

标签:stun   eof   添加   create   html   str   cto   继承   var   

原文地址:http://www.cnblogs.com/luowenjun-kio/p/7899962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!