zoukankan      html  css  js  c++  java
  • js的小随笔

    1、在js中{  }中的块级语句没有独立的作用域

    var i = 5;for(; i < 8; i++){ console.log(i); } //输出 5 6 7
    //全局设置的变量i在for条件中也能拿到
    if(true){ var a = 5; } console.log(a);    //输出5
    //if条件中设置的变量a在全局中也能拿到

    2、函数中是有作用域的,函数内的变量在函数外不能被访问

    function f1(){ var x = 7; } f1(); console.log(typeof x);
    //输出undefined

    3、函数中用连等定义变量,除第一个变量外其他变量都是全局作用域,若需要定义多个变量建议使用逗号

    function f1(){ var x = y = 6; } f1(); console.log(typeof y);console.log(typeof x);
    //输出 number undefined
    function f1(){ var x,y = 6; } f1(); console.log(typeof y);console.log(typeof x);
    //输出undefined undefined

    4、函数声明和函数表达式

    函数声明:function fun(){  }函数声明会被预先处理,所以可以在之前调用。

    fun();function fun(){ console.log("say hello!"); }
    //输出 say hello!

    函数表达式:var fun = function(){  }函数表达式不能被预先处理,所以不能在赋值前调用

    fun(); var fun = function(){ console.log("say hello!"); }
    
    //报错: fun is not a function
  • 相关阅读:
    密码保护
    实现搜索功能
    完成个人中心—导航标签
    个人中心标签页导航
    评论列表显示及排序,个人中心显示
    完成评论功能
    从首页问答标题到问答详情页
    首页列表显示全部问答,完成问答详情页布局
    Android基础学习:Android环境搭建
    liunx 硬盘分区
  • 原文地址:https://www.cnblogs.com/MirageFox/p/5731254.html
Copyright © 2011-2022 走看看