型继承、类和继承

2018-04-23 14:53:00
admin
原创
396
<!DOCTYPE html>
<html>
<head>
    <title>原型继承、类和继承</title>
</head>
<body>
    <p>原型继承、类和继承</p> 
	<script type="text/javascript">
		//es5 原型继承
        function Person(name,age,email) {  
            this.user_name = name;  
            this.user_age = age;  
            this.user_email = email;  
  
        }  
        Person.prototype.sayHi=function(){  
            alert ('原型继承: '+this.user_name+'    '+this.user_age+'   '+this.user_email);  
        }  
        var p1=new Person('原型继承',18,'ss@163.com');  
        p1.sayHi();  
        function student(sid) {  
            this.user_ID = sid;  
        }  
        student.prototype = p1;  
        var s1 = new student('001');  
        s1.user_name = 'sss';  
        s1.user_age = 10;  
        s1.sayHi();  
		
	//es6 类和继承
	class PersonClass {  
          constructor(name,age,email){
			this.user_name = name;  
            this.user_age = age;  
            this.user_email = email;  
		  }
		  sayHi(){  
            alert ('类方法: '+this.user_name+'    '+this.user_age+'   '+this.user_email);  
          }  
    }
	class StudentClass extends PersonClass {
		  constructor(name,age,email,sid){
			super(name,age,email);
			this.user_ID = sid;  
          }
		  sayHi2(){  
            alert ('子类方法: '+this.user_name+'    '+this.user_age+'   '+this.user_email);  
          }  
	}
	var s2 = new StudentClass('子类继承',18,'ss@163.com','002');
	s2.user_name = '子类';  
    s2.user_age = 20;  
    s2.sayHi();
	s2.sayHi2();	

    </script>
</body>
</html>
文章分类
联系我们
电话: 18902328227
Email: 2689701946@qq.com
QQ: 2689701946
微信: 18902328227
地址: 广州番禺区繁华路
  • 在线客服
  • 关注微信
  • 联系电话
    • 欢迎咨询
    • 欢迎加入QQ群 303257430
  • 公众号:源宝网络
    • 联系电话及微信号 18902328227