型继承、类和继承

2018-04-23 14:53:00
admin
原创
609
<!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>
  • QQ讨论群
  • 关注微信
    • 欢迎咨询
    • 欢迎加入QQ群 16506021
  • 公众号:源宝网络