JavaScript 基础

 

目录

 

js的变量

js作用域(scope)

数据类型

类型检测和类型转换


js的变量

  • 变量提升

    1.用var定义的变量会变量提升

    2.定义的函数会变量提升

    3.在函数体内,直接定义变量 num=100,在全局作用域下也可以访问

 // 变量提升:用var定义变量,变量会提升到顶部,值为undefined
     console.log(num)
     var num = 100;


    /**********************/

    //var 变量提升
    var num;
    console.log(num)
    num = 100;

    console.log(num)
    num = 100;


    /*********************************/
    // var num=100;          //③全局变量,在任何位置可以访问
    function Demo() {

      // var num = 100;      //①局部变量
      num = 100;          //②定义的全局变量,在函数外部可以访问

    }
    Demo()
    console.log(num)


    /****************************/

    // 函数也会提升
    Demo()          //100

    function Demo() {
      num = 100;
      console.log(num)

    }

 命名规范

  1.1 大驼峰

 /**********大驼峰************/
    class PersonAddress {

    }

    new PersonAddress()

    /*****构造函数******/
    function Person(){

    }

     2.2小驼峰

 	function getBookCount() {

    }

    function setName(){
      //设置

    }

    function isEnglish(){
      //判断用is开头
    }


    function createCar(){

    }

    function canRead(){
      //能不能
    }


    function hasBook(){
      //判断有没有
    }

js作用域(scope)

  • 全局作用

  •       变量的提升 

  • <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <script>
    
        // 变量提升:用var定义变量,变量会提升到顶部,值为undefined
        // console.log(num)
        // var num = 100;
    
    
        /**********************/
    
        //var 变量提升
        var num;
        console.log(num)
        num = 100;
    
        console.log(num)
        num = 100;
    
    
        /*********************************/
        // var num=100;          //③全局变量,在任何位置可以访问
        function Demo() {
    
          // var num = 100;      //①局部变量
          num = 100;          //②定义的全局变量,在函数外部可以访问
    
        }
        Demo()
        console.log(num)
    
    
        /****************************/
    
        // 函数也会提升
        Demo()          //100
    
        function Demo() {
          num = 100;
          console.log(num)
    
        }
    
    
    
    
    
    
      </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    

  • 函数作用域

  • <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <script>
    
        // let没有变量提升
        // console.log(num)
        // let num = 100;
    
        // const没有变量提升
        // console.log(NUM)
        // const NUM=100;
        // console.log(NUM)
    
        /********小驼峰:自定义函数************/
        function getBookCount() {
    
        }
    
        function setName(){
          //设置
    
        }
    
        function isEnglish(){
          //判断用is开头
        }
    
    
        function createCar(){
    
        }
    
        function canRead(){
          //能不能
        }
    
    
        function hasBook(){
          //判断有没有
        }
    
    
        /**********大驼峰************/
        class PersonAddress {
    
        }
    
        new PersonAddress()
    
        /*****构造函数******/
        // function Person(){
    
        // }
    
    
        function demo() {
          console.log(this)
        }
        // demo()      //普通函数:this指向window
        let p = new demo()    //构造函数(实例化):this指向实例化对象
    
    
      </script>
    </head>
    
    <body>
    
    </body>
    
    </html>

  • 块作用域

  • <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <script>
    
        /************改变this指向*
         * call   立即执行
         * apply   立即执行,参数放到一个数组里
         * bind   不会立即执行
         * *************/
        function Demo(uname, age) {
          // console.log(this)
          console.log(uname, age)
        }
    
        //call  bind apply
        let obj = {}
    
    
        // Demo.call(obj, "张三", 21)        //改变this指向,并立即执行
    
        // let f = Demo.bind(obj, "张三", 21)            //改变this指向,不立即执行
        // f()                                 //执行新的函数
    
    
        Demo.apply(obj, ["张三", 21])             //改变this指向,并立即执行;参数放到数组里
    
    
    
    
    
      </script>
    </head>
    
    <body>
    
    </body>
    
    </html>

    for循环

    white循环

    if嵌套

数据类型

  • 基础数据类型:
  • 复杂数据类型

类型检测和类型转换

  • 类型检测常用方法
  • 显式类型转换

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值