javascript学习笔记
// 打印
console.log('hello world')
console.error('this is an error')
console.warn('this is a warn')
//var, let, const 声明变量
const age = 30;
age = 33
console.log(age)
String, Numbers, Boolean, null, undefined 数据类型
const name = 'john';
const age = 30;
const x = null;
const y = undefined;
console.log(typeof x);
// 变量拼接
const name = 'John';
const age = 30;
console.log('my name is ' + name + ' and i am ' + age)
console.log(`my name is ${name} and i am ${age}`)
// 字符串方法
const s ='Hello, World'
console.log(s.length)
console.log(s.toUpperCase()) //大写
console.log(s.toLocaleLowerCase()) //小写
console.log(s.substring(0,4)) //截取
console.log(s.split(', ')) //分割成数组
// 操作数组方法
const numbers = new Array('apples', 'oranges')
const numbers = ['apples','oranges']
numbers.push('agee') //添加到数组尾部
numbers.unshift('one') //添加到数组首部
numbers.pop() //删除最后一个数值
console.log(numbers[0])
console.log(Array.isArray(numbers)) //判断是否为数组
console.log(numbers.indexOf('apples')) //查看一个数在数组中的位置
console.log(numbers)
// 对象方法
const person = {
fistName : 'John',
lastName : 'Doe',
age : 30,
hobbies : ['music', 'moives', 'sports'],
address : {
street : '50 main st',
city : 'boston',
state : 'MA'
}
}
console.log(person.fistName,person.address.city) //提取对象中的值
const {fistName, lastName, address:{city}} = person;
console.log(fistName,city)
person.email = 'hy546880109@qq.com' //给对象添加键值对
console.log(person)
// for循环
for(let i=0; i<10;i++){
console.log(`number:${i}`)
}
// while循环
let i=0;
while(i<10)
{
i++;
console.log(i)
}
// for循环读取json文本
const todos = [
{
id: 1,
text: 'take out trash ',
isCompleted: true
},
{
id: 2,
text: ' out ',
isCompleted: true
},{
id: 3,
text: 'take ',
isCompleted: true
}
]
for(let i=0;i<todos.length;i++){
console.log(`number:${todos[i].id}`)
console.log(`text:${todos[i].text}`)
}
// forEach函数遍历json文本
todos.forEach(function(todo){
console.log(todo.text)
})
// map函数遍历json文本
const todoText = todos.map(function(todo){
return todo.text
})
console.log(todoText)
// 判断语句
const x = 4
if(x===10){
console.log('x is 10')
}else if(x>10){
console.log('x > 10')
}else{
console.log('x is null')
}
// 多重判断
const x = 10;
if(x >5 && x <8){
console.log('x is true')
}else{
console.log('x is false')
}
// 三元运算符
const x = 10
const color = x > 10 ? 'red' : 'blue'
console.log(color)
// switch判断
switch(color){
case 'red':
console.log('color is red')
break
case 'blue':
console.log('color is blue')
break
default:
console.log('color is not red or bule')
break
}
// 函数
function addNums(num1=1,num2=2){
console.log(num1+num2)
}
addNums()
const address = (num1=1,num2=2)=> {
console.log(num1+num2)
}
address()
function Person(firsName, lastName, dob){
this.firsName = firsName
this.lastName = lastName
this.dob = new Date(dob)
this.getBirthYear = function(){
return this.bod.getFullYear()
}
this.getFullName = function(){
return `${this.firsName} ${this.lastName}`
}
}
// 类方法
class Person{
constructor(firsName, lastName, dob){
this.firsName = firsName
this.lastName = lastName
this.dob = new Date(dob)
}
getBirthYear(){
return this.bod.getFullYear()
}
getFullName(){
return `${this.firsName} ${this.lastName}`
}
}
const person1 = new Person('John','Doe','4-30-1980')
const person2 = new Person('Mary','Smith','3-6-1970')
console.log(person1.getFullName())
console.log(person1)