JS ES6学习笔记

// For应用---------------------------------------------
const todos =
[
{
id : 1,
text : 'Take out trash',
isCompleted : true
},
{
id : 2,
text : 'Meeting with boss',
isCompleted : true
},
{
id : 3,
text : 'Dentist appt',
isCompleted : false
},
];
for(let i=0; i<todos.length; i++)
{
console.log('todos[i].text:', todos[i].text);
}
//简洁用法
for(let t of todos)
{
console.log('t.id:',t.id);
}
// forEach, map, filter--------------------------------------------
// forEach
todos.forEach //也可以写成箭头函数
(
function(t) // 这是回调函数
{
console.log('t.text:', t.text);
}
);
// map
const t = todos.map //map返回一个数组
(
function(t)
{
// console.log('t.text:', t.text);
return t.id === 1;
// return t.id;
}
);
console.log('t:', t);
// filter 过滤器
const tCompleted = todos.filter //Completed 完整的
(
function(t)
{
return t.isCompleted === true; // === 相当于python的 ==
}
);
console.log('tCompleted:', tCompleted)
// map和filter的区别:前者是返回数组,后者是返回符合条件的数组
const tttCompleted = todos.filter
(
function(t)
{
return t.isCompleted === true;
}
).map(function(t){return t.text;})
console.log('tttCompleted:', tttCompleted)
// javascript常用变量类型:
// Numbers, String, Boolean, Object: Array, Undefined, Null
const name = 'John'; // String const age = 22; // Numbers const rating = 4.5; // Numbers, 没有浮点类型,只是数字 const isCool = true; // Boolean const x = null; // Object let z = [1,2,3]; //Object const y = undefined; // undefined
/* let和const的
共同点是:1、不可重复声明 2、都是代码块作用域
不同点是:const是常量,定义赋值后,不可改变
var与let、const特性完全相反 */





// 逻辑运算演示---------------------------------------------------
// false 是 undefined, 0, "", null, false
// true 是 除了上面的,都是true
// ----------- || 或 演示 -----------
const xx = 11;
if(xx<6 || xx>10) // ||某一个是true,结果为true
{
console.log('逻辑‘或’:成立')
}else
{ console.log('逻辑‘或’:不成立') }
// ----------- && 与 演示 -----------
const yy = 11;
if(yy>1 && yy<10) // &&需要两个条件都是true,结果才是true
{
console.log('逻辑‘与’:成立')
}else
{ console.log('逻辑‘与’:不成立') }
// 三元操作符 --------------
const xxx = 9;
const color = xxx > 10 ? 'red' : 'blue';
//如果问号后面条件为真,设置color为red,冒号代表else
console.log('xxx color :', color)


浙公网安备 33010602011771号