<script>
// 利用 new Object()创建对象
var obj1 = new Object();
// 利用对象字面量创建对象
var obj2 = {};
// 利用构造函数创建对象
function Star(uname, age) {
this.uname = uname;
this.age = age;
this.sing = function() {
console.log('我会唱歌');
}
}
var ldh = new Star('刘德华', 18);
</script>


prototype属性,指向另一个对象,注意这个prototype就是一个对象,这个对象的所有属性和方法,都会被构造函数所拥有。
__proto__(左右两边各有两个下划线)指向构造函数的prototype原型对象,之所以我们对象可以使用构造函数prototype原型对象的属性和方法,就是因为对象有__proto__原型的存在。

__proto__)和构造函数(prototype)原型对象里面都有一个属性constructor属性,constructor我们称为构造函数,因为它指回构造函数本身。




Array.prototype = {},只能是Array.prototype.xxx = function() {}的方式
fun.call(thisArg, arg1, arg2, ...)
call()把父类型的this指向子类型的this,这样就可以实现子类型继承父类型的属性。


// 想让变量每次加一
// i = i + 1; // 开始
// i++; // 现在
// 把数组元素翻转过来
// 原来:利用for循环
// 现在: 利用reverse

forEach()、map()、filter()、some()、every();array.forEach(function(currentValue, index, arr))currentValue:数组当前项的值index:数组当前项的索引arr:数组对象本身
array.filter(function(currentValue, index, arr))currentValue:数组当前项的值index:数组当前项的索引arr:数组对象本身
array.some(function(currentValue, index, arr))currentValue:数组当前项的值index:数组当前项的索引arr:数组对象本身
1.filter也是查找满足条件的元素,但它返回的是一个数组,而且是把所有满足条件的元素返回回来;
2.some也是查找满足条件的元素是否存在,但它返回的是一个布尔值,如果查找到第一个满足条件的元素就终止循环。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>查询商品案例title>
<style>
.search {
width: 600px;
margin: 30px auto;
}
input {
width: 65px;
padding: 0;
margin: 0;
}
input:focus {
outline: 2px solid #51c9ff; /* outline是绘制于元素周围的一条线,位于边框边缘的外围,不会占据空间 */
border: 1px solid #fff;
border-radius: 2px;
}
button {
background-color: #51c9ff;
color: #fff;
border: none;
border-radius: 3px;
width: 60px;
height: 25px;
}
table {
width: 600px;
border: 1px solid #000;
border-collapse: collapse; /* 边框会合并为一个单一的边框 */
margin: 20px auto;
}
th,
td {
border: 1px solid #ccc;
text-align: center;
height: 50px;
}
tr:hover {
background-color: whitesmoke;
}
style>
head>
<body>
<div class="search">
按照价格查询: <input type="text" class="start"> - <input type="text" class="end">
<button class="search_price">搜索button>
按照商品名称查询: <input type="text" class="product">
<button class="search_pro">查询button>
div>
<table>
<thead>
<tr>
<th>idth>
<th>产品名称th>
<th>价格th>
tr>
thead>
<tbody>
tbody>
table>
<script>
// 1.把数据渲染到页面中(删除tbody中的内容,通过数据的方式进行渲染)
// 利用新增数组方法操作数据
var data = [
{
id: 1,
pname: '小米',
price: 3999
}, {
id: 2,
pname: 'oppo',
price: 999
}, {
id: 3,
pname: '荣耀',
price: 1299
}, {
id: 4,
pname: '华为',
price: 1999
}
];
// 利用forEach()遍历数据进而渲染数据
// (1)获取相应的元素
var tbody = document.querySelector('tbody');
// (2)将数据渲染到页面中
/* data.forEach(function(value) { // 此时value是每一个对象
var tr = document.createElement('tr'); // 先创建一个行
tr.innerHTML = ''+ value.id +' '+ value.pname +' '+ value.price +' ';
tbody.appendChild(tr); // 将行追加到tbody里面
}) */
setDate(data);
// 由于多次渲染所需要的代码相似,于是将上述渲染数据的代码放入一个函数中,但由于每次参数不同(第一次为data 第二次为newDate),因此借用形参mydata
function setDate(mydata) {
// 先清空原来tbody里面的数据
tbody.innerHTML = '';
// 再渲染
mydata.forEach(function(value) { // 此时value是每一个对象
var tr = document.createElement('tr'); // 先创建一个行
tr.innerHTML = ''+ value.id +' '+ value.pname +' '+ value.price +' ';
tbody.appendChild(tr); // 将行追加到tbody里面
})
}
// 2.根据价格显示数据
// 利用filter来做
// 当我们点击了按钮,就可以根据我们的商品价格去筛选数组里面的对象
// (1)获取相应的元素
var search_price = document.querySelector('.search_price');
var start = document.querySelector('.start');
var end = document.querySelector('.end');
// (2)绑定事件
search_price.addEventListener('click',function() {
var newDate = data.filter(function(value) {
return value.price >= start.value && value.price <= end.value;
});
console.log(newDate);
// (3)将筛选之后的对象渲染到页面中
setDate(newDate);
})
// 3.根据商品名称显示数据
// 利用some来做
// 如果查询数组中唯一的元素,用some方法更合适,因为它找到这个元素,就不再进行循环,效率更高
// (1)获取相应元素
var search_pro = document.querySelector('.search_pro');
var product = document.querySelector('.product');
// (2)绑定事件
search_pro.addEventListener('click',function() {
var arr = [];
data.some(function(value) {
if(value.pname === product.value) {
arr.push(value); // 将对象数据添加到数组中
return true; // reture 后面必须写true
}
});
// (3)将拿到的数据渲染到页面中
// 它返回的是对象,我们需要以数组的形式来进行渲染,因此我们需要声明一个数组
setDate(arr);
})
script>
body>
html>



trim()方法会从一个字符串的两端删除空白字符。str.trim()trim()方法并不影响字符串本身,它返回的是一个新的字符串。

Object.defineProperty()定义对象中新属性或修改原有的属性。Object.defineProperty(obj, prop, descriptor)obj:必需。目标对象prop:必需。需定义或修改的属性的名字descriptor:必需。目标属性所拥有的特性
