学习javascript第二天

1.创建类和对象

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
	</body>
</html>

<script>
//先把猴王传递给uname ,uname又传给this.uname  this指向创建的实例
//创建类     //constructor构造函数
class Star{
	constructor(uname,age){
		this.uname=uname;    //指向创建的实例
		this.age=age;
	}
}
//利用类生成对象 new
var houwang = new Star('猴王',18);
var houzi = new Star('猴子',17);
console.log(houwang)
console.log(houzi.uname+':'+houzi.age);
</script>

2.类中共有方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
	</body>
</html>

<script>
//先把猴王传递给uname ,uname又传给this.uname  this指向创建的实例
//创建类     //constructor构造函数
class Star{
	constructor(uname,age){
		this.uname=uname;    //指向创建的实例
		this.age=age;
	}
	chi(xiangjiao){
		console.log(this.uname+'吃'+xiangjiao);
	}
}
//利用类生成对象 new
var houwang = new Star('猴王',18);
var houzi = new Star('猴子',17);
console.log(houwang)
console.log(houzi.uname+':'+houzi.age);
houwang.chi('大香蕉'); //调用chi这个方法
houzi.chi('小香蕉');
</script>

3.类的继承和super关键字的使用

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
	</body>
</html>
<script>
class Star{
	constructor(x,y){
		this.x=x;
		this.y=y;
	}
	houwang(){
		console.log(this.x+this.y);
	}
}
class houzi extends Star{
		constructor(x,y){
			super(x,y);//调用了父类的构造函数
		}
	}
var text = new houzi(1313,1);
var text1 = new houzi(249,1);
text.houwang(); 
text1.houwang();
</script>

4.猴爷爸孙继承

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	</body>
</html>
<script>
class houye{
	constructor(x,y){
		this.x=x;
		this.y=y;
	}
	eat(){
		console.log('猴爷吃'+(this.x+this.y+1)+'根香蕉');
	}
}
class houba extends houye{
	constructor(x,y){
		super(x,y);
		this.x=x;
		this.y=y;
	}
	eat1(){
		console.log('猴爸吃'+(this.x+this.y)+'根香蕉');
	}
}
class houer extends houba{
	constructor(x,y){
		super(x,y);
		this.x=x;
		this.y=y;
	}
	eat2(){
		console.log('猴儿吃'+(this.x+this.y-1)+'根香蕉');
	}
}
var test = new houer(5,5);
 test.eat();
 test.eat1();
 test.eat2();
</script>

5.this指向问题

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<button>点击</button>
	</body>
</html>
<script>
var that;
var _that;
class houwang{
	constructor(name,age){
		that=this
		//constructor 里面的this指向的创建的实例对象
		this.name = name;
		this.age = age;
		this.btn = document.querySelector('button');
		this.btn.onclick = this.houhou;
		console.log(this);
		;
	}
	houhou(){
		//这个houhou里面的this指向的是btn这个按钮,因为这个按钮调用了这个函数
		console.log(this);
		console.log(this.houhou);//因为this指向的是btn 然后btn没有这个实例
	console.log(that.name);//输出猴王
	}
	houer(){
		//这个houer里面的this指向的是实例对象 test 因为test调用了这个函数
		_that=this; 
  console.log(this);
	}
}
var test = new houwang('猴王');
console.log(that === test);   //检测that与猴王是不是一样
test.houer();
console.log(_that === test);
</script>

6.增删改小案例

<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>面向对象 Tab</title>
		<style>
			@font-face {
				font-family: "iconfont";
				src: url('./iconfont/iconfont.eot?t=1553960438096');
				/* IE9 */
				src: url('./iconfont/iconfont.eot?t=1553960438096#iefix') format('embedded-opentype'),
					/* IE6-IE8 */
					url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAK4AAsAAAAABmwAAAJrAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCCcAp4fwE2AiQDCAsGAAQgBYRtBzAbpQXIrrApw71oi3CCOyzEy8RvE4yIN8TD036/zp03qCYRjaJZNBFFS/gREoRGipQKofjuNrb+9XbTqrmXcqWzfTRDqFqWkhAJzYToaE6LQ7Q30CirRqSKMnj58DdIdrNAdhoTQJa5VGfLrtiAy+lPoAcZdUC57UljTR4TMAo4oL0xiqwYG8YueIHPCdTqYajty/t+bUpmrwvEnUK42lQhLMssVy1UNhzN4kmF6vSQVvMY/T5+HEU1SUXBbti7uBBrx++cgqJULp0GhAgBna5AgSkgE0eN6R1NwTitNt0yAI5VG7wr/8AljmoX7K+zq+tBF1Q8k9JTPWp1AjnJDgCzmM3bU0V31dsvV3M2eC6fHjaGfX/qS7U5Gr58vj6uD0bgxudyrV/OtHHyP+NZnpO1txbktjdY+3FB61+7nxeOzq8niGYnRwT3v3aZxeXf6rrNxl5//49WlEtZUUL1Pj3Bv1EO7MuG2namrCkbvcnApLUJtWpRhv2tzlRLx43kQ7WO2/FW6c5QqDZEZnYKFeosoVK1NdSa5E/XaVM1Ra7BhAEQmk0kjV5QaLbIzG5U6HRRqTkK1DqJtivrjMT1zJaNnIsihAiyQE3JdbszcW0Xiadzdl4d8UO0HSUGNDNXzl2hifYSO5pPjrorgdjUAAavoa5TKDZVUXD3kuuOOzh70fShvUiN2owtNsRxIREIIiATUCYpGO2aqXy/CxEeHcfuaKrLDiGbQ5kcEMsNIK8M5qCmR3mn8RFHOpcECBtlAAwWIZ2OAqV5kQoJXHvShORYBzrDZKhhb3uT8QPlrA3bmsKZV6i89DiTV2o1AAAA') format('woff2'),
					url('./iconfont/iconfont.woff?t=1553960438096') format('woff'),
					url('./iconfont/iconfont.ttf?t=1553960438096') format('truetype'),
					/* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
					url('./iconfont/iconfont.svg?t=1553960438096#iconfont') format('svg');
				/* iOS 4.1- */
			}

			.iconfont {
				font-family: "iconfont" !important;
				font-size: 16px;
				font-style: normal;
				-webkit-font-smoothing: antialiased;
				-moz-osx-font-smoothing: grayscale;
			}

			.icon-guanbi:before {
				content: "\e676";
			}


			* {
				margin: 0;
				padding: 0;
			}

			ul li {
				list-style: none;
			}

			main {
				width: 960px;
				height: 500px;
				border-radius: 10px;
				margin: 50px auto;
			}

			main h4 {
				height: 100px;
				line-height: 100px;
				text-align: center;
			}

			.tabsbox {
				width: 900px;
				margin: 0 auto;
				height: 400px;
		 	border: 1px solid lightsalmon;
				position: relative;
			}

			nav ul {
				overflow: hidden;
			}

			nav ul li {
				float: left;
				width: 100px;
				height: 50px;
				line-height: 50px;
				text-align: center;
				border-right: 1px solid #ccc;
				position: relative;
			}

			nav ul li.liactive {
				border-bottom: 2px solid #fff;
				z-index: 9;
			}

			#tab input {
				width: 80%;
				height: 60%;
			}

			nav ul li span:last-child {
				position: absolute;
				user-select: none;
				font-size: 12px;
				top: -18px;
				right: 0;
				display: inline-block;
				height: 20px;
			}

			.tabadd {
				position: absolute;
				/* width: 100px; */
				top: 0;
				right: 0;
			}

			.tabadd span {
				display: block;
				width: 20px;
				height: 20px;
				line-height: 20px;
				text-align: center;
				border: 1px solid #ccc;
				float: right;
				margin: 10px;
				user-select: none;
			}

			.tabscon {
				width: 100%;
				height: 300px;
				position: absolute;
				padding: 30px;
				top: 50px;
				left: 0px;
				box-sizing: border-box;
				border-top: 1px solid #ccc;
			}

			.tabscon section,
			.tabscon section.conactive {
				display: none;
				width: 100%;
				height: 100%;
			}

			.tabscon section.conactive {
				display: block;
			}

			.liactive {
				background: pink;
			}
		</style>
	</head>

	<body>

		<main>
			<h4>
				Js 面向对象 动态添加标签页
			</h4>
			<div class="tabsbox" id="tab">
				<!-- tab 标签 -->
				<nav class="fisrstnav">
					<ul>
						<li class="liactive"><span>测试1</span><span class="iconfont icon-guanbi"></span></li>
						<li><span>测试2</span><span class="iconfont icon-guanbi"></span></li>
						<li><span>测试3</span><span class="iconfont icon-guanbi"></span></li>
					</ul>

					<div class="tabadd">
						<span>+</span>
					</div>
				</nav>

				<!-- tab 内容 -->
				<div class="tabscon">
					<section class="conactive">测试1</section>
					<section>测试2</section>
					<section>测试3</section>
				</div>

			</div>
		</main>
	</body>
</html>
<script>
	var that;
	class Tab {
		constructor(id) {
			that = this;
			//获取元素
			this.main = document.querySelector(id); //拿到tab的id
			this.add = this.main.querySelector('.tabadd');
			this.ul = this.main.querySelector('.fisrstnav ul:first-child');
			this.tabneirong = this.main.querySelector('.tabscon');
			this.init()
		}
		init() {
			this.update();
			this.add.onclick = this.addTab;
			for (var i = 0; i < this.lis.length; i++) {
				this.lis[i].index = i;
				// this.lis[i].onclick=function(){
				// 	console.log(this.index);
				// }
				//加了括号会一运行就调用  所以不加括号
				this.lis[i].onclick = this.toggleTab;
				this.remove[i].onclick=this.removeTab;
				this.spans[i].ondblclick = this.editTab;
				this.sections[i].ondblclick = this.editTab;
			}
		}
		update(){//解决后添加的元素没有获取到   动态添加元素重新获取
			this.lis = this.main.querySelectorAll('li');
			this.sections = this.main.querySelectorAll('section');
			this.remove = this.main.querySelectorAll('.icon-guanbi');//获取删除按钮
			this.spans = this.main.querySelectorAll('.fisrstnav li span:first-child');
		}
		//1.切换功能
		toggleTab() {
			that.clearclass(); //先执行清空操作再添加。
			// console.log(this.index);
			this.className = 'liactive';
			that.sections[this.index].className = 'conactive';
		}
		clearclass() {
			/*清除函数*/
			for (var i = 0; i < this.lis.length; i++) {
				this.lis[i].className = '';
				this.sections[i].className = '';
			}
		}
		// 2. 添加功能
		addTab() {
			that.clearclass();
			var random = Math.random();
			var li = '<li class="liactive"><span>测试1</span><span class="iconfont icon-guanbi"></span></li>';
			var section ='<section class="conactive">测试'+random+'</section>';
			that.ul.insertAdjacentHTML('beforeend', li);
			that.tabneirong.insertAdjacentHTML('beforeend',section);
			that.init();
		}
		// 3.删除功能
		removeTab(e) {
			e.stopPropagation();//防止事件冒泡
			var index=this.parentNode.index;/*获取他父亲的索引号*/
			console.log(index);
			//根据索引号来删除 
			that.lis[index].remove();
			that.sections[index].remove();
			that.init()//重新获取下标
			//删除的不是选定状态的li的时候,原来的选中状态li保持不变
			if(document.querySelector('.liactive')) return;//如果找到这个属性  return则后面的不执行
			//删除后上一个标签直接处于选定状态
			index--;
			//onclick手动点  click自动点   前面个为真&&后面的语句执行
			that.lis[index]&&that.lis[index].click();//索引号为0时报错
		}
		// 4.修改功能
		editTab() {
			//双击不选中文字
		// 	window.getSelection ? window.getSelection().removeAllRanges():document.selection.empty();
		// alert('f');
		var str = this.innerHTML;
		this.innerHTML='<input type="text">';//双击添加文本框
		var input = this.children[0];//文本框是他的第一个孩子
		input.value=str;
		input.select();//文本框里面的文字处于选定状态
		//当我们离开文本框里面的值给span
		input.onblur = function(){//离开函数
			this.parentNode.innerHTML=this.value;//input中value的值给他的父亲span
		}
		//按下回车键执行一样执行操作
		input.onkeyup = function(e){
			if(e.keyCode===13){
				this.blur();
			}
		}
		}
	}
	new Tab('#tab'); /*实例化对象*/
</script>

7.prototype原型对象(实现方法的共享)

 

8.原型对象扩展内置对象

9.call改变指向的使用

10.foreach遍历和filter筛选和some查找

11.查询商品小案例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			table {
				border: 1px solid pink;
				margin: 250px auto;
				transform: scale(4, 4);
			}

			td {
				text-align: center;
			}

			th {
				background-color: aqua;
				padding: 0 25px 0 25px;
			}

			#cont {
				height: 100px;
				background-color: green;
				margin-top: 130px;
			}

			#cont div {
				margin-left: 200px;
			}

			#cont p {
				line-height: 100px;
				font-size: 40px;
				display: inline;
				margin-left: 10px;
			}

			#cont span {
				line-height: 100px;
				font-size: 40px;
				display: inline;
				margin-left: 30px;
			}

			#cont input {
				display: inline;
				height: 50px;
				position: relative;
				top: -10px;
				left: 10px;
			}

			#cont button {
				display: inline;
				height: 50px;
				position: relative;
				top: -10px;
				left: 20px;
			}
		</style>
	</head>
	<body>
		<div id="cont">
			<div>
				<p>按照价格查询</p><input type ="text"class="start">
				<p>-</p>
				<input type="text" class="end"><button type="button" class="sousuo">搜索</button>
				<span>按照商品名称查询</span><input type="text" class="product">
				<button type="button" class="chaxun">查询</button>
			</div>
		</div>
		<table>
			<thead>
				<tr>
					<th>编号</th>
					<th>产品</th>
					<th>价格</th>
				</tr>
			</thead>
			<tbody>

			</tbody>
		</table>
	</body>
</html>
<script>
	var data = [{
		id: 1,
		name: '小米',
		price: 999
	}, {
		id: 2,
		name: '华为',
		price: 1299
	}, {
		id: 3,
		name: '苹果',
		price: 1699
	}, {
		id: 4,
		name: '三星',
		price: 1999
	}, ];
	//1.获取相应的元素
	var tbody = document.querySelector('tbody');
	var sousuo = document.querySelector('.sousuo');
	var start = document.querySelector('.start');
	var end = document.querySelector('.end');
	var product = document.querySelector('.product');
	var chaxun = document.querySelector('.chaxun');
	setdata(data);//第一次打开页面先把data里面的数据传mydata进去
	
	//2.把数据渲染到页面中
	function setdata(maydata){
		maydata.forEach(function(value) {
		
			var tr = document.createElement('tr');
			tr.innerHTML = '<td>' + value.id + '</td><td>' + value.name +
				'</td><td>' + value.price + '<td>';
			tbody.appendChild(tr);
		});
	}
	//3.根据价格查询商品
	sousuo.addEventListener('click',function(){
		var newdata = data.filter(function(value){
			return value.price>=start.value&&value.price<=end.value;
		});
		// console.log(newdata);
		//把筛选完的数组对象放进去调用  生成一个新的表格
		setdata(newdata);
	});
	//4.根据名称来查找商品
	//如果查询数组中唯一的元素  就用some方法  因为一查找到这个元素就退出循环  效率更高
	chaxun.addEventListener('click',function(){
		var arr=[];
		data.some(function(value){
			if(value.name===product.value){
				arr.push(value);
				return true;//终止遍历  提高效率
			}
		});
		setdata(arr);
	})
</script>

12.trim清除左右空格

13.Object.defineProperty

14.object.keys

 

15.调用函数的几种方式

 16.call和apply和bind

 

17.高阶函数和闭包

 

 18.闭包的小案例

1///

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="ll">
			<li>1</li>
			<li>2</li>
			<li>3</li>
			<li>4</li>
		</div>
	</body>
</html>
<script>
	//闭包应用点击哪个li打印出序列号
	//1.利用动态添加属性的方式 
	var  lis = document.querySelector('#ll').querySelectorAll('li');
	for(var i = 0 ; i < lis.length ; i++){
		lis[i].index = i;
		lis[i].onclick = function (){
			console.log(this.index);
		}
	}
	//2.利用闭包的方式获得每个li的序列号
	for(var i = 0 ;i<lis.length;i++){
		//利用for循环创建了四个立即执行函数
		//立即执行函数也称为小闭包  因为立即执行函数里面每一个函数都可以使用他的变量i
		(function(i){
			lis[i].onclick = function(){
				console.log(i);
			}
		})(i);
	}
</script>

 2///

 3///

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	</body>
</html>
<script>
//打车车费计算  3公里起步价10 超过的5元一公里计算 
//如果拥堵    额外添加拥堵费10元
var kk = (function(){
	var start = 10 //起步价
	var total;  //总价
	return {
		//正常的总价
		price:function(n){
			if(n<3){
				 total = start;
			}else{
				total = ( n-3 )*5 + start;
			}
			return total;
		},
		yongdu:function(flag){
			return flag ? total+10:total;
		}
	}
})();
console.log(kk.price(5)+'元');
console.log(kk.yongdu(true)+'元');  //拥堵
console.log(kk.price(3)+'元');
console.log(kk.yongdu(false)+'元');   //不拥堵
</script>

4///小思考题(全局作用域下挂载在window上)

没有访问到局部变量所以没有闭包的产生

 

19.递归函数

 ///1.求阶层

 ///2.递归遍历数据

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	</body>
</html>
<script>
	var data = [{
		id: 1,
		name: '家电',
		goods: [{
			id: 12,
			gname: '冰箱',
			goods:[{
				id: 111,
				gname: '彩电1',
			},{
				id: 222,
				gname: '彩电2',
			}]
		}, {
			id: 13,
			gname: '彩电3',
		}]
	}, {
		id: 2,
		name: '电视'
	}];
	//输入id获取对象
	function getid(json, id) {
		var h = {};
		json.forEach(function(item) {
			if (item.id == id) {   // 判断外层
				h = item;
				//我们想要得到里面的数据 11 12 利用递归函数
				//里面应该有goods这个数组  并且数组长度不为0
			}else if(item.goods && item.goods.length > 0){
				 h = getid(item.goods,id);
			}
			
		})
		return h;
	}
	console.log(getid(data, 1));
	console.log(getid(data, 2));
	console.log(getid(data, 12));
	console.log(getid(data, 13));
	console.log(getid(data, 111));
	console.log(getid(data, 222));
</script>

19.浅拷贝和深拷贝

 

20.documents

21.网页全选和取消全选小案列

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		男:<input type="checkbox" name="checkbox">
		女:<input type="checkbox" name="checkbox">
		男:<input type="checkbox" name="checkbox">
		女:<input type="checkbox" name="checkbox">
		<br>
		<button onclick="checked('checkbox')">全选</button>
		<button onclick="unchecked('checkbox')">取消全选</button>
	</body>
</html>
<script>
function checked(objname){
	var objnamelist = document.getElementsByName(objname);
	if(null!=objnamelist){
		for(var i = 0;i<objnamelist.length;i++){
			objnamelist[i].checked="checked";
		}
	}
}
function unchecked(objname){
	var objnamelist = document.getElementsByName(objname);
	if(null!=objnamelist){
		for(var i = 0;i<objnamelist.length;i++){
			objnamelist[i].checked="";
		}
	}
}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值