JavaScript程序由语句组成、语句遵守特定的语法规则。例如:if语句,while语句,with语句等等。
一.block语句、var语句
1.block语句
block语句常用于组合0-多个语句。块语句用一对花括号定义。
语法:
{
语句1;
语句2;
...
语句n;
}
如:
{
var str = "hi";
console.log(str);
}
或
if(true) {
console.log('hi');
}
注意:
1.console中输入{a:1,b:2}会报错:SyntaxError:Unexpected token:因为被浏览器理解为块了。应该:var o = {a:1,b:2};
2.没有块级作用域,如下两种写法是等价的:
而如下则是函数作用域:
function foo() {
var a = 1;
console.log(a);//1
}
foo();
console.log(typeof a);//undefined
2.var语句
function foo() {
var a=b=1;
}
foo();
console.log(typeof a);//'undefined'
console.log(typeof b);//'number'
正确做法:
function foo() {
var a=1,b=1;
}
foo();
console.log(typeof a);//'undefined'
console.log(typeof b);//'undefined'
二.try-cath语句
1.
try {
throw "test";
}catch(ex) {
console.log(ex);//test
}finally {
console.log('finally');
}
运行结果:
test finally
2.
try {
//do sth
}finally {
console.log('finally');
}
运行结果:
finally
3.
try {
try {
throw new Error("oops");
}finally {
console.log("finally");
}
}catch(ex) {
console.error("outer", ex.message);
}
运行结果:
finally outer oops
4.
try {
try {
throw new Error("oops");
}catch(ex) {
console.error("inner", ex.message);
}finally {
console.log("finally");
}
}catch(ex) {
console.error("outer", ex.message);
}
运行结果:
inner oops finally
5.
try {
try {
throw new Error("oops");
}catch(ex) {
console.error("inner", ex.message);
throw ex;
}finally {
console.log("finally");
}
}catch(ex) {
console.error("outer", ex.message);
}
运行结果:
inner oops finally outer oops
三.函数、swith、循环
1.函数
函数声明
fd();//true
function fd() {
//do sth.
return true;
}
函数表达式
fe();//TypeError
var fe = function() {
//do sth.
};
2.swith
a.
var val = 2;
switch(val) {
case 1:
console.log(1);
break;
case 2:
console.log(2);
break;
default:
console.log(0);
break;
}
//2
b.
var val = 2;
switch(val) {
case 1:
console.log(1);
case 2:
console.log(2);
default:
console.log(0);
}
//2
//0
c.
val = 2;
switch(val) {
case 1:
case 2:
case 3:
console.log(123);
break;
case 4:
case 5:
console.log(45);
break;
default:
console.log(0);
}
//123
3.循环
for...in
var p;
var obj = {x:1,y:2}
for(p in obj) {
}
for...in特点:
a).顺序不确定
b).enumerable为false时不会出现
c).for in对象属性时受原型链影响
4.with语句
with({x:1}) {
console.log(x);
}
//1
或
with(document.forms[0]) {
console.log(name.value);
}
或
var form = document.forms[0];
console.log(form.name.value);
实际上,在JavaScript中不建议使用with了,因为:
a.让JS引擎优化更难
b.可读性差
c.可被变量定义代替
d.严格模式下被禁用
四.严格模式
严格模式是一种特殊的执行模式,它修复了部分语言上的不足,提供更强的错误检查,并增强安全性。
1.让函数里的代码在严格模式下运行:
function func() {
'use strict';
}
2.在整个文件的最开头部分写‘use strict',这样这个文件内的所有JS代码都在严格模式下执行
'use strict';
function func() {
}
当然,在'user strict'也可以写’abc'等其它语句,但不可以写var a;之类的。
3.严格模式的特点
a).不允许用with
b).所有变量必须声明,赋值给未声明的变量报错,而不是隐式创建全局变量
c).eval中的代码不能创建eval在作用域下的变量、函数。而是为eval单独创建一个作用域,并在eval返回时丢弃
d).函数中的特殊对象arguments是静态副本,而不像非严格模式那样,修改arguments或修改参数变量会相互影响
e).删除configurable=false的属性时报错,而不是忽略
f).对象字面量重复属性名报错
g).禁止八进制字面量,如010(八进制的8)
h).eval,arguments变为关键字,不可作为变量名、函数名等
i).一般函数调用时(不是对象的方法调用,也不使用apply/call/bind等修改this)this指向null,而不是全局对象
j).若使用apply/call,当传入null或undefined时,this将指向null或undefined,而不是全局对象
k).试图修改不可写属性(writable=false),在不可扩展的对象上添加属性时报TypeError,而不是忽略
l).arguments.caller,arguments.callee被禁用