本来昨天就应该发布出去的。审核没有通过。作为一个新人,应该是那里没有做好,触犯了行为规范条例。已经加以整改并在以后注意。
数据类型的转换
- Number()
let lastYear = '1991';
let yearLast = Number(lastYear);
console.log(lastYear + 8, yearLast + 8);
运行的结果
- String()
let lastYear = 1991;
let yearLast = String(lastYear);
console.log(lastYear + 8, yearLast + 8);
运行的结果
- Boolean()
只有 0 ‘’ undefined null NaN 为false,其余全为true
console.log(Boolean(0));
console.log(Boolean(''));
console.log(Boolean(undefined));
console.log(Boolean(null));
console.log(Boolean(NaN));
- 强制类型转换
console.log('23' - '10' - 3);
console.log('23' + '10' + 3);
console.log('24' * '3');
console.log('24' / '3');
当运算符无法工作时,才会进行强制类型转换。
逻辑运算符
- &&
不多做解释 以下代码与结果及其清晰。
const a = true;
const b = true;
const c = false;
const d = false;
console.log(a&&b);
console.log(a&&c);
console.log(a&&d);
console.log(c&&d);
- || 同样清晰
const a = true;
const b = true;
const c = false;
const d = false;
console.log(a||b);
console.log(a||c);
console.log(a||d);
console.log(c||d);
- ! 取反没啥说的
!false = true
判断语句
-
if else 前面说过,最简单也是最广泛使用的判断语句。
-
switch case语句
在一个变量可能有多种情况的值时用的较多。总体来说使用较少。
注: 结尾要加入break;
- 三元表达式
a>b?1:2
意思是如果a>b条件成立,则返回1,否则返回2
function基本使用(会有箭头函数)
- 函数声明(带参调用)
function fruitProcessor(apples, oranges) {
console.log(apples, oranges);
const juice = apples+oranges;
return juice
}
const fruit = fruitProcessor(5,2)
console.log(fruit);
- 函数表达式
const calcAge2 = function(birthYeah){
return 2022 - birthYeah;
}
const age2 = calcAge2(1991);
console.log(age2);
与函数声明非常相似,看个人喜好去使用。特定的开发环境选择最适合的就可以了。
- 箭头函数
const calcAge3 = birthYeah => 2022 - birthYeah
const age3 = calcAge3(2001);
console.log(age3);
写法与函数表达式极其类似,可以理解为简化型的函数表达式。但再Vue框架中需要特别注意,在function和箭头函数中使用this又很大区别。
function函数中this指向Vue实例对象。
箭头函数中this指向window。
这个非常的重要。初学时在这方面可谓吃尽了苦头。
数组
定义数组的两种方法
const friends = ['Peter','Michael','Steven']; // 常用 const years = new Array(1991,1984,2008,2020); // !常用
每个数组会自动生成它的长度(length),并且要灵活的使用数组的索引而完成我们所要完成的需求
数组的常用方法
- push() 可以添加一个元素在数组末尾
- unshift() 可以添加一个元素在数组开头
- pop() 可以删除数组中末尾元素
- shift() 可以删除数组开头元素
- indexOf() 可以查找数组已知元素在数组中的索引。如若元素不存在则返回-1
- includes() ES6的新元素,如若数组中存在输入元素,即返回true,否则返回false。
注: 此方法不会进行强制类型转换
对象
对象基本格式
const aaa = {
name:'ys',
age:'21',
address:'tianjin'
}
即{keys:values,...,...,...}
调用对象里面的keys时有两种常用方法
- 代替点表示法
console.log(aaa.name);
- 括号表示法
console.log(aaa['name']);
返回的结果与代替点表示法相同但括号表示法可以做到更多事情
const bbb = prompt()
console.log(aaa.bbb);
代替点表示法输入name返回undefined,而括号表示发会返回相应的value,但在我们实际工作中还是使用代替点表示法居多,因为他更加的清晰。
今天就看到这了。写的东西有点多。明天加油