AI智能
改变未来

javascript基础学习笔记

javascript学习笔记// 打印console.log(\'hello world\')console.error(\'this is an error\')console.warn(\'this is a warn\')//var, let, const 声明变量const age = 30;age = 33console.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 = 4if(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 = 10const color = x > 10 ? \'red\' : \'blue\'console.log(color)// switch判断switch(color){case \'red\':console.log(\'color is red\')breakcase \'blue\':console.log(\'color is blue\')breakdefault: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 = firsNamethis.lastName = lastNamethis.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 = firsNamethis.lastName = lastNamethis.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)
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » javascript基础学习笔记