Javascript笔记02:严格模式的特定要求
1.严格模式变量必须声明,不然会报错:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>严格模式</title> </head> <body> <script type="text/javascript"> "use strict"; try { i = 1; }catch(err) { alert(err); } </script> </body> </html>
这里i没有使用var修饰,就是没有定义的意思,后抛出"undeclared variable i"错误
2.严格模式下,不能删除全局变量、函数和函数的参数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>严格模式</title> </head> <body> <script type="text/javascript"> "use strict"; var i; function myfunc() {}; delete i;//语法错误 delete myfunc();//语法错误
function myfunc2(arg)
{
delete arg;//语法错误
}
</script>
</body>
</html>