<script>
/******************* es6 *****************************/
//es6 ``(windows tab ) 。
let str = ' ';
const message = ` ${str}` //
console.log(message)
function fn1() {
return 'i am fn1'
}
var fn2 = function () {
return 'i am fn2'
}
console.log(`${fn1()} xxx ${fn2()}`) //i am fn1 xxx i am fn2
//
let str2 = `
,
`
console.log(str2)
let str3 = " <br>"
+ " <br>"
+ " <br>"
+ " <br>"
console.log(str3)
// yyyy-MM-dd hh:mm:ss
let dt = new Date().toLocaleString('cn', { hour12: false });
console.log(dt);
//
let arr = [1, true, "hello"];
console.log(arr.toString())
const bruce = { name: ' ' }
function update(birthYear, occupation) {
this.birthYear = birthYear;
this.occupation = occupation;
}
//call
update.call(bruce, 1949, 'dsadsad')
console.log(bruce); //{name: " ", birthYear: 1949, occupation: "dsadsad"}
//apply ( )
update.apply(bruce, [1949, 'dsadsad'])
console.log(bruce); //{name: " ", birthYear: 1949, occupation: "dsadsad"}
const arr1 = [2, 3, -5, -11, 9];
console.log(Math.min.apply(null, arr1)); // -11
console.log(Math.max.apply(null, arr1)); //9
//
let peoples = [
{ id: 1000, name: ' ', age: 21 },
{ id: 1001, name: ' ', age: 18 },
{ id: 1002, name: ' ', age: 20 },
{ id: 1003, name: ' ', age: 21 },
]
console.log(peoples.find(p => p.id === 1002)); //{id: 1002, name: " ", age: 20}
console.log(peoples.find(p => p.id === 5000)); //undefined
console.log(peoples.findIndex(p => p.id === 1002)); //2
console.log(peoples.findIndex(p => p.id === 5000)); //-1
let arr2 = [1, 2, 3].map(function (item) {
return `<li>${item}</li>` //['<li>1</li>','<li>2</li>','<li>3</li>']
})
console.log(arr2.join('')) //<li>1</li><li>2</li><li>3</li>
console.log("---------------------------------------------------")
//some , , true
console.log(peoples.some(p => p.age === 21)); //true
//every , true, , , false
console.log(peoples.every(p => p.age === 21)); //true
/******************** map filter **************************/
console.log(peoples.map(p => p.name)) //[" ", " ", " ", " "]
console.log(peoples.map(p => p.age + 10)) //[ 31, 28, 30, 31 ]
peoples.map(function (item, index, arr) {
console.log('value :', item); //10 20 30
console.log('index :', index); //0 1 2
})
//
console.log(peoples.filter(p => p.age === 18)); //[{ id: 1001, name: " ", age: 18 }]
console.log("---------------------------------------------------")
const users = peoples.map(item => ({
userId: item.id,
userName: item.name,
userAge: item.age,
})
);
console.log(users)
//reduce
/*
arr.reduce(callback,[initialValue])
callback ( , )
previousValue ( , (initialValue))
currentValue ( )
index ( )
array ( reduce )
initialValue ( callback 。 )
*/
var items = [10, 120, 1000];
var reducer = function add(sumSoFar, item) { return sumSoFar + item; };
console.log(items.reduce(reducer, 0)); //1130
console.log(items.reduce((a, x) => a += x)); //1130
console.log(items.reduce((a, x) => a += x, 0)); //1130
console.log(items.reduce((a, x) => a += x, 10)); //1140
//
console.log(items.reduce((a, b) => a + b) / items.length); //376.6666666666667
// peoples
var pItems = peoples.map(p => p.age);
console.log(pItems.reduce((a, b) => a + b) / pItems.length); //20
//
for (let item of peoples) {
console.log(item)
}
console.log("---------------------------------------------------")
Object.keys(peoples).forEach(p => console.log(peoples[p]))
//console.log(peoples.filter(p => p.age.match(/^2/)));
//js
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '(' + this.x + ', ' + this.y + ')';
}
add() {
return this.x + this.y
}
}
var point = new Point(1, 2);
console.log(point)
console.log(point.add())
console.log("---------------------------------------------------")
//map
let userMaps = new Map([
[1, 'one'],
[2, 'two'],
[3, 'three'],
[1, 'one'],
[3, 'three'],
]);
console.log(userMaps)
//try...catch....finally
try {
foo.bar()
} catch (e) {
console.log(e.message)
} finally {
console.log(' ')
}
//
const input = "i am going to shenzhen";
const reg = /\w{3,}/ig //i g
console.log(reg.test(input)) // true
console.log(input.match(/\w{3,}/ig)) // ["going", "shenzhen"]
console.log(input.replace(/\w{3,}/ig, '***')) //i am *** to ***
</script>
</code></pre>
<p> </p>
</div>
</div>
</div>
</div>