展开全部
注意:在javascript语言中,key的值只能是字符串,不能是其它的。
在javascript中,对象本身就是一种Map结构62616964757a686964616fe4b893e5b19e31333366306436。
var map = {};
map['key1'] = 1;
map['key2@'] = 2;
console.log(map['key1']);//结果是1.
console.log(map['key2@']);//结果是2.
//如果遍历map
for(var prop in map){
if(map.hasOwnProperty(prop){
console.log('key is ' + prop +' and value is' + map[prop]);
}
}
动态的赋值key:
var key1 = '动态key1';
var key2 = '动态key2';
var map = {};
map[key1] = 1;
map[key2] = 2;
console.log(map[key1]);//结果是1.
console.log(map[key2]);//结果是2.
//如果遍历map
for(var prop in map){
if(map.hasOwnProperty(prop){
console.log('key is ' + prop +' and value is' + map[prop]);
}
}
扩展资料
JavaScript是一种脚本语言,其源代码在发往客户端运行之前不需经过编译,而是将文本格式的字符代码发送给浏览器由浏览器解释运行。
直译语言的弱点是安全性较差,而且在JavaScript中,如果一条运行不了,那么下面的语言也无法运行。而其解决办法就是于使用try{}catch(){}︰
console.log("a");//这是正确的
console.log("b");//这是正确的
console.logg("c");//这是错误的,并且到这里会停下来
console.log("d");//这是正确的
console.log("e");//这是正确的
/*解决办法*/
try{console.log("a");}catch(e){}//这是正确的
try{console.log("b");}catch(e){}//这是正确的
try{console.logg("c");}catch(e){}//这是错误的,但是到这里不会停下来,而是跳过
try{console.log("d");}catch(e){}//这是正确的
try{console.log("e");}catch(e){}//这是正确的
Javascript被归类为直译语言,因为主流的引擎都是每次运行时加载代码并解译。
V8是将所有代码解译后再开始运行,其他引擎则是逐行解译(SpiderMonkey会将解译过的指令暂存,以提高性能,称为实时编译)。
但由于V8的核心部份多数用Javascript撰写(而SpiderMonkey是C++)。因此在不同的测试上,两者性能互有优劣。
与其相对应的是编译语言,例如C语言,以编译语言编写的程序在运行之前,必须经过编译,将代码编译为机器码,再加以运行。