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 = 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)

__EOF__

本文作者Harry
本文链接https://www.cnblogs.com/harry66/p/14136309.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   Harry_666  阅读(102)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2019-12-15 selenium元素定位不到问题分析及解决办法
2019-12-15 python3基础3
2019-12-15 python3基础2
2019-12-15 python3基础1
点击右上角即可分享
微信分享提示