ES6面向对象

ES6面向对象

ES6之前的面向对象如何写的,想必大家都知道大部分都是凑出来的。先看看之前的代码面向对象该怎么写?

<script type="text/javascript">
		function User(name,pass){
			this.name=name;
			this.pass=pass;
		}
		User.prototype.showName=function(){
			alert(`姓名:${this.name}\n密码:${this.pass}`);
		}
		let li=new User('zhangsan','4599332');
		li.showName();

		</script> 
ES6面向对象

1.增加了class关键字,构造器和类分开了
2.class里面直接加方法—————————

<script type="text/javascript">
	class User{
				constructor(name,pass){
					this.name=name;
					this.pass=pass;
				}
				showName(){
					alert(`姓名:${this.name}\n密码:${this.pass}`);	
				}
			}
	
			let li=new User('zhangsan','4599332');
			li.showName();
			
		</script>

ES6之前的继承

function User(name,pass){
			this.name=name;
			this.pass=pass;
		}
		User.prototype.showName=function(){
			alert(`姓名:${this.name}\n密码:${this.pass}`);
		}
		

		
		function VipUser(name,pass,level){
			User.call(this,name,pass);
			this.level=level;
		}
		VipUser.prototype=new User();
		VipUser.prototype.constructor=VipUser;
		VipUser.prototype.showLevel=function (){
			alert(this.level);
		}
		let v1=new VipUser('lisi','33445566','3');
		v1.showName();
		v1.showLevel();

ES6的继承

class User{
			constructor(name,pass){
				this.name=name;
				this.pass=pass;
			}
			showName(){
				alert(`姓名:${this.name}\n密码:${this.pass}`);
			}
		}

		class VipUser extends User{
			constructor(name,pass,level){
				super(name,pass);
				this.level=level;
			}
			showLevel(){
				alert(this.level);
			}
		}
		let v1=new VipUser('lisi','4599332','99');
		v1.showName();
		v1.showLevel();
ES6是不是更方便啊!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值