<!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>es6 增强对象字面量</title>
</head>
<body>
<script>
/*
* 增强对象字面量
* 解决问题:缩减代码
*/
function createBookShop (inventory) {
return {
inventory,
/* inventoryValue: function () {
return this.inventory.reduce((total,book) => {
return total + book;
},0)
}, */
//简写
inventoryValue () {
return this.inventory.reduce((total,book) => total + book.price,0);
},
priceForTitle (title) {
return this.inventory.find(book => book.title === title).price;
}
}
}
const inventory = [
{title:"Vue",price:10},
{title:"Angular",price:20}
];
const bookShop = createBookShop(inventory);
console.log(bookShop.inventoryValue());
console.log(bookShop.priceForTitle("Angular"));
// 假实例
function saveFile(url,data) {
$.ajax({
url, // url:url
data, // data:data
method:"POST"
});
}
const url = "http://fileupload.com0";
const data = {color:"red"};
saveFile(url,data);
</script>
</body>
</html>