js变量,json格式编码困惑

来源:互联网 发布:完美刀塔助手没有网络 编辑:程序博客网 时间:2024/06/05 23:51

json格式编码困惑

#1

map:{'username':"input[name='username']",

          'password':"input[name='password']",
          'ok':'#dijit_form_Button_0'

        }

#2

map:{username:"input[name='username']",
          password:"input[name='password']",
          ok:'#dijit_form_Button_0'
        }

#1与#2使用上没有什么不一样。对js来说key都字符串。但如果你不想给key有规范的命名,那最好使用引号。

例:

test = {1:'error key'};

test.1 #error key

--------------------

test = {'1':'right key'};

test.1 #no access way

test['1']#right access way


test = {'1/!#$%^3':'right key'};

test.1/!#$%^3 #no access way

test['1/!#$%^3']#right access way




=======规范==========

test = {'key':'right key'};

test.key#no access way

test['key']#right access way


变量困惑:

定义变量时有var与没有var有区别么?

答案是:有!!

var test = 9;

function t(){

test = 10;

console.log(test)#10

};

console.log(test)#10

----------------------------

var test = 9;

function t(){

var test = 10;

console.log(test)#10

};

console.log(test)#9

当没有var时,js不能确定当前是定义新的变量test还是使用已有的变量test(此时,会在先在本域scope里找,如果没有找到就逐级往上找,直到windows全局,如果还没有找到就会在当前scope里新定义变量test).

所以规范使用是:当定义新的变量时,要使用var,这样不会修改别人的变量。



0 0
原创粉丝点击