es6 class类小demo

使用如下

在这里插入图片描述

代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<style>
	</style>
	<body>
	</body>
</html>
<script src="jquery.min.js"></script>
<script>
	class demo {
     
     
		constructor(id) {
     
     
			this.domInit(id);
			this.a = $('#'+id);
		}
		domInit(id) {
     
     
			$('body').append('<div id="'+id+'" style="width: 100px;height: 100px;background-color: #009688;"></div>')
		}
		alert() {
     
     
			alert('我是alert')
		}
		show() {
     
     
			this.a.show();
		}
		hide() {
     
     
			this.a.hide()
		}
		changeColor() {
     
     
			let c1 = Math.floor(Math.random() * 255)
			let c2 = Math.floor(Math.random() * 255)
			let c3 = Math.floor(Math.random() * 255)
			this.a.css('background-color', 'rgb(' + c1 + ',' + c2 + ',' + c3 + ')')
		}
	}
</script>

猜你喜欢

转载自blog.csdn.net/qq_42068550/article/details/115320754