一、检测数据类型的四种方法
1、typeof
2、instanceof
检测某个实例是不是隶属某个类
**
var ary=[1,2,3];
ary instanceof Array;//true
ary instanceof Object;//true
3、constructor
constructor可以手动改变 (或者手动更改原型指向的话,检测就不准确了)
**
function fn () {}
undefined // 返回值
fn.constructor = 1
1 // 返回值
fn.constructor;
1 // 打印值
var a = 1;
undefined
a.constructor;
ƒ Number() { [native code] }
4、Object.prototype.toString.call();
Object.prototype.toString.call(1);
"[object Number]"
Object.prototype.toString.call(false);
"[object Boolean]"
Object.prototype.toString.call("");
"[object String]"
Object.prototype.toString.call(null);
"[object Null]"
Object.prototype.toString.call(undefined);
"[object Undefined]"
Object.prototype.toString.call(function () {});
"[object Function]"
Object.prototype.toString.call({});
"[object Object]"
var obj = {};
undefined
obj.toString.call(123);
"[object Number]"
window.toString.call(undefined);
"[object Undefined]"
toString.call(null);
"[object Null]"
二、模拟购物车的案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>计算机</title>
<style>
* {
margin: 0;
padding: 0;
}
ul,ol {
list-style: none;
}
.main {
width: 500px;
height: 600px;
background-image: url(../day16/img-cmp/bg1.png);
background-size: cover;
color: cyan;
padding: 20px;
box-sizing: border-box;
margin: 10px auto;
}
.main .head {
height: 150px;
}
.main ul {
height: 300px;
}
.main li {
display: flex;
justify-content: center;
align-items: center;
margin: 10px;
box-sizing: border-box;
}
.main button {
width: 52px;
height: 44px;
border: none;
background: #000;
}
.main .sub {
background-image: url(../day16/img-cmp/sub.png);
background-size: cover;
}
.main .num {
display: inline-block;
width: 52px;
height: 44px;
line-height: 44px;
background-color: #fff;
border-radius: 5px;
text-align: center;
color: maroon;
}
.main .add {
background-image: url(../day16/img-cmp/add.png);
background-size: cover;
}
.shopInfo .big {
display: none;
}
</style>
</head>
<body>
<div class="main">
<div class="head"></div>
<ul>
<li>
<button class="sub" flag = "1"></button>
<b class="num">0</b>
<button class="add" flag = "0"></button>
<span class="info">
单价:<b class="itemPrice">1</b> 小计:<b class="xjMoney">0</b>元
</span>
</li>
<li>
<button class="sub" flag = "1"></button>
<b class="num">0</b>
<button class="add" flag = "0"></button>
<span class="info">
单价:<b class="itemPrice">3</b> 小计:<b class="xjMoney">0</b>元
</span>
</li>
<li>
<button class="sub" flag = "1"></button>
<b class="num">0</b>
<button class="add" flag = "0"></button>
<span class="info">
单价:<b class="itemPrice">5</b> 小计:<b class="xjMoney">0</b>元
</span>
</li>
<li>
<button class="sub" flag = "1"></button>
<b class="num">0</b>
<button class="add" flag = "0"></button>
<span class="info">
单价:<b class="itemPrice">7</b> 小计:<b class="xjMoney">0</b>元
</span>
</li>
<li>
<button class="sub" flag = "1"></button>
<b class="num">0</b>
<button class="add" flag = "0"></button>
<span class="info">
单价:<b class="itemPrice">9</b> 小计:<b class="xjMoney">0</b>元
</span>
</li>
</ul>
<div class="shopInfo">
<p>商品共<span class="totalNum">0</span>jian</p>
<p>商品共花费<span class="totalPrice">0</span>yuan</p>
<p class="big">最贵商品价格<span class="bigPrice">0</span>yuan</p>
</div>
</div>
<script src="./3计算机2.js"></script>
</body>
</html>
var buttons = document.querySelectorAll(".main button");
var numArray = document.querySelectorAll(".num");
var priceArray = document.querySelectorAll(".xjMoney");
var lis = document.querySelectorAll(".main li");
var bigPrice = document.querySelector(".bigPrice");
var big = document.querySelector(".big");
for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function () {
big.style.display = "block";
var flag = this.getAttribute("flag");
var parentEle = this.parentNode;
var itemPrice = Number(parentEle.querySelector(".itemPrice").innerText);
var xjMoney = parentEle.querySelector(".xjMoney");
var num = parentEle.querySelector(".num");
var oldNum = Number(num.innerText);
var nowNum = 0;
// 总个数
var tNum = 0;
var totalNum = document.querySelector(".totalNum");
// 总价格
var tPrice = 0;
var totalPrice = document.querySelector(".totalPrice");
var maxPrice = 0;
var newAry = [];
if (flag == 0) {
nowNum = oldNum + 1;
} else {
if (oldNum - 1 < 0) {
nowNum = 0;
return;
}
nowNum = oldNum - 1;
}
num.innerText = nowNum;
xjMoney.innerText = itemPrice * nowNum;
// 计算总商品个数、总价格
for (var i = 0; i < numArray.length; i++) {
tNum += Number(numArray[i].innerText);
// console.log(numArray[i]);
tPrice += Number(priceArray[i].innerText);
}
totalNum.innerText = tNum;
totalPrice.innerText = tPrice;
// 找出最大商品单价
for (var i = 0; i < lis.length; i++) {
if (lis[i].querySelector(".num").innerText > 0) {
newAry.push(lis[i].querySelector(".itemPrice").innerText);
}
}
if (newAry.length < 1) {
maxPrice = "-----";
} else {
var bigAry = newAry.sort(function (a, b) {
return a - b;
});
maxPrice = bigAry[bigAry.length - 1];
// console.log(maxPrice);
}
bigPrice.innerText = maxPrice;
}
}