Getting Started with JavaScript - For Notes


与html和css不同的是js并不是一门标记语言,而是与java等相同,也是一门编程语言(实现人机交互的运行在客户端即浏览器的编程语言)

JavaScript function:

  • 1. Web page special effects (monitor user behavior and let the web page make corresponding feedback)
  • 2. Form verification (judging the legality of form data)
  • 3. Data interaction (get back-end data, render to front-end)
  • 4. Server programming (node.js)

js composition

Output syntax:

  • Document output content: document.write('xxxxx')
  • Page popup: alert('xxxxxx')
  • Console log printout: console.log('xxxxx')

Input syntax:

  • Page pop-up prompt box: prompt('xxxxxx')
    alert()与prompt()会跳过页面渲染先被执行

variable

存储数据的容器

The use of var is not recommended, and important things should be emphasized. It is not recommended to use var. It has almost been eliminated at present.

//声明变量:let 变量名
let age
//赋值:变量名 = 赋值
age = 18
console.log(age)
//变量初始化
let name = 芋头

array

//数组初始化: let 数组名 = [数据1 , 数据2 , 数据3]
let ages = [18 , 19 , 20]
//splice(x , y)从x下标开始删除y个元素
ages.splice(0 , 1)

constant

const声明表示内存地址不可改变,当声明为数组时,数组中数据仍可改变哦

 const PI = 3.14

template string

外面用反引号里面变量“${变量名}”

let age = 22
document.write(`芋头今年${
      
      age}岁了  `)

Detect data type

//typeof 数据
let age = null 
typeof age

judgment

" == " : Values ​​are equal
" === " : Exactly equal
" != " : Values ​​are not equal
" !== " : Not equal at all

if (1 != '1'){
    
    
	console.log('相等')
} else {
    
    
	console.log('不相等')
}

branch judgment

case做的判断是全等于判断哦~~

	let num1 = +prompt("输入第一个数")
   switch (num1){
    
    
    case 1: 
      alert("一") 
      break
    case 2:
      alert("二")
      break
    default:
      alert("不是一和二")
      break
      
   }

cycle

while、do…while ; for
break:退出整个循环 ; continue:结束本次循环继续下一次循环

 		let num = 0
        while (num < 10){
    
    
            document.write(`现在这个数是${
      
      num}<br>`)
            num ++
        }
        do {
    
    
            document.write(`现在这个数是${
      
      num}<br>`)
            num ++
        } while (num < 20)
        for (;num < 30 ; num ++){
    
    
            document.write(`现在这个数是${
      
      num}<br>`)
        }

loop result

array

  • Add to
    let arr = ['张飞', '赵云']
    // 新增  push 推末尾并返回该数组的新长度
    console.log(arr.push('黄忠'))  
    arr.push('马超', '关羽')
    console.log(arr)
    // 开头追加 并且返回数组新长度
    arr.unshift('吕布')
    console.log(arr)
  • delete
 let arr = [0 , 1 , 2 , 3 , 4 , 5]
    //pop():删除最后一个元素并返回该元素的值
    arr.pop()
    console.log(arr)
    //shift():删除第一个元素并返回元素的值
    arr.shift()
    //splice(start , deleteCount):从start位置开始删除deleteCount个元素
    arr.splice(0 , 2)

function

//交换a , b的值
    let a = 10 
    let b = 20
    console.log(`a 现在的值是:${
      
      a};b现在的值是:${
      
      b}`)
    serve(a,b)
    function serve(a , b) {
    
    
      let temp = a 
      a = b
      b = temp
      console.log(`a 现在的值是:${
      
      a};b现在的值是:${
      
      b}`)
    }
    //实际不会换,实参与形参问题

函数内部不声明直接复制的变量会当作全局变量处理

    let num = 10 
    function fun (){
    
    
	    //let num = 20   局部变量使用不影响num
 	    num = 20     //全局变量会影响num的值
    }
    fun()
    console.log(num)

函数在查找变量时会一层一层向外查找

Of course, I have to mention the immediate execution function here, not much to say as shown in the following code

//立即执行函数不需要调用会直接执行函数里的内容
(function(){
    
    
	console.log("这是一个立即执行函数")
})();

(function(a , b){
    
    
	console.log(a + b)
})(10 , 20);

object

    let man = {
    
    
      name: "芋头",
      num: 22,
      sex: "男",
      powers: function (a) {
    
    
        console.log(`make ${
      
      a} fly`)
      }
    }
    console.log(man.name)
    console.log(man["sex"])
    console.log(man.powers("person"))
    delete man.sex//删除属性

Guess you like

Origin blog.csdn.net/weixin_45696320/article/details/131778756