2017.8.17变量提升、函数提升、作用域、json格式

变量提升

会经常用到,主要要理解javascript中的级作用域

var v='Hello World';
alert(v);//Hello World
var v='Hello World';
(function(){
    alert(v);//Hello World
})()
var v='Hello World';
(function(){
    alert(v);
    var v='I love you';//undefined
})()

js中的是函数级作用域,这和C语言等中的有很大的不同(块级作用域)。在javascript中,块级,就像if语句,并不会创建一个新的作用域,只有函数才会创建新的作用域
C语言

#include 
    int main() {
    int x = 1;
    printf("%d, ", x); // 1
    if (1) {
        int x = 2;
        printf("%d, ", x); // 2
     }
    printf("%d\n", x); // 1
}

javascript

 var x = 1;
    console.log(x); // 1
 if (true) {
   var x = 2;
   console.log(x); //2
}
 console.log(x);// 2

函数提升

简而言之,就是将整个函数提升到前面去。
js中写函数的两种方式

  • 函数表达式
  • 函数申明方式
    注意:只支持函数申明方式
function myTest(){
    foo();
    function foo(){
        alert("我来自 foo");
    }
}
myTest();
function myTest(){
    foo();
   var foo =function foo(){
        alert("我来自 foo");
    }
}
myTest();

状态码

  • 404请求失败,请求所希望得到的资源在服务器上没有找到

json数组

json数组就是一个包含多个json数据的数组,也可以包含json数组

[{"a":"b","c":5500},{"a":"av8d","c":600},{"JSON教程":"http://www.sojson.com/json/"}]
//json数组

另外json对象注意点:

//合格的json格式
["one", "two", "three"]

{ "one": 1, "two": 2, "three": 3 }

{"names": ["张三", "李四"] }

[ { "name": "张三"}, {"name": "李四"} ]
//都是不合格的json数据格式
{ name: "张三", 'age': 32 }  // 属性名必须使用双引号

[32, 64, 128, 0xFFF] // 不能使用十六进制值

{ "name": "张三", age: undefined } // 不能使用undefined

{ "name": "张三",
  "birthday": new Date('Fri, 26 Aug 2011 07:13:10 GMT'),
  "getName": function() {
      return this.name;
  }
} // 不能使用函数和日期对象

按钮的切换:涉及知识点v-show,v-if,v-else

你可能感兴趣的:(2017.8.17变量提升、函数提升、作用域、json格式)