029_一元运算符

1. 一元运算符只有一个参数, 即要操作的对象或值。它们是JavaScript中最简单的运算符。

2. 一元加法(+)

2.1. 一元加法对数字无作用; 数字对象转为原始数字; NaN还是NaN。

2.2. 一元加法把null转为数字0。

2.3. 一元加法把空字符串(''和new String())转为数字0。

2.4. 一元加法把空数组([]和new Array())转为数字0。

2.5. 一元加法把true、new Boolean(true)转为数字1, false、new Boolean(false)转为数字0。

2.6. 一元加法把可完整转为数字的字符串转为数字; 八进制的字符串忽略前导0, 然后按十进制转换; 字符串NaN转为数字NaN。

2.7. 一元加法把undefined、不能完整转为数字的字符串、对象(不包括空字符串对象(new String())、可完整转为数字的字符串对象(new String('8'))、空数组对象([]和new Array())、布尔对象(new Boolean(true)和new Boolean(false))、数字对象(new Number()构造函数创建的数字对象))转为数字NaN。

2.8. 一元加法并没有改变运算数本身的类型。

2.9. 例

2.9.1. 代码

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<title>一元加法</title>
	</head>
	<body>
		<script type="text/javascript">
			var ua = undefined;
			var la = null;
			var ba = true, bb = false, bc = new Boolean(true), bd = new Boolean(false);
			var na = 0, nb = 1, nc = -10, nd = 0x10, ne = 010, nf = +10, ng = 3.1415926, nh = 5e-324, ni = 1.7976931348623157e+308;
			var nj = NaN, nk = Infinity, nl = -Infinity, nm = new Number(1000);
			var sa = '', sb = '1abc', sc = 'ABC1', sd ='16', se = '3.1415926', sf = '3.14.15926', sg = '+10', sh = '-10', si = '0x10', sj = '010', sk = '5e-324';
			var sl = '1.7976931348623157e+308', sm = 'Infinity', sn = '-Infinity', so = 'NaN', sp = new String(), sq = new String('8');
			var aa = [], ab = ['高性能JavaScript'], ac = new Array();
			var oa = {}, ob = {id: 1001, name: 'zhangsan'}, oc = new Object();

			document.write('一元加法对数字无作用; 数字对象转为原始数字: <br />');
			document.write('na = ' + na + ', +na = ' + +na + ', na的类型: ' + typeof na + ', +na的类型: ' + typeof +na + '<br />');
			document.write('nb = ' + nb + ', +nb = ' + +nb + ', nb的类型: ' + typeof nb + ', +nb的类型: ' + typeof +nb + '<br />');
			document.write('nc = ' + nc + ', +nc = ' + +nc + ', nc的类型: ' + typeof nc + ', +nc的类型: ' + typeof +nc + '<br />');
			document.write('nd = ' + nd + ', +nd = ' + +nd + ', nd的类型: ' + typeof nd + ', +nd的类型: ' + typeof +nd + '<br />');
			document.write('ne = ' + ne + ', +ne = ' + +ne + ', ne的类型: ' + typeof ne + ', +ne的类型: ' + typeof +ne + '<br />');
			document.write('nf = ' + nf + ', +nf = ' + +nf + ', nf的类型: ' + typeof nf + ', +nf的类型: ' + typeof +nf + '<br />');
			document.write('ng = ' + ng + ', +ng = ' + +ng + ', ng的类型: ' + typeof ng + ', +ng的类型: ' + typeof +ng + '<br />');
			document.write('nh = ' + nh + ', +nh = ' + +nh + ', nh的类型: ' + typeof nh + ', +nh的类型: ' + typeof +nh + '<br />');
			document.write('ni = ' + ni + ', +ni = ' + +ni + ', ni的类型: ' + typeof ni + ', +ni的类型: ' + typeof +ni + '<br />');
			document.write('nj = ' + nj + ', +nj = ' + +nj + ', nj的类型: ' + typeof nj + ', +nj的类型: ' + typeof +nj + '<br />');
			document.write('nk = ' + nk + ', +nk = ' + +nk + ', nk的类型: ' + typeof nk + ', +nk的类型: ' + typeof +nk + '<br />');
			document.write('nl = ' + nl + ', +nl = ' + +nl + ', nl的类型: ' + typeof nl + ', +nl的类型: ' + typeof +nl + '<br />');
			document.write('nm = ' + nm + ', +nm = ' + +nm + ', nm的类型: ' + typeof nm + ', +nm的类型: ' + typeof +nm + '<hr />');

			document.write('一元加法把null转为数字0: <br />');
			document.write('la = ' + la + ', +la = ' + +la + ', na的类型: ' + typeof la + ', +na的类型: ' + typeof +la + '<hr />');

			document.write('一元加法把空字符串(\'\'和new String())转为数字0: <br />');
			document.write('sa = ' + sa + ', +sa = ' + +sa + ', sa的类型: ' + typeof sa + ', +sa的类型: ' + typeof +sa + '<br />');
			document.write('sp = ' + sp + ', +sp = ' + +sp + ', sp的类型: ' + typeof sp + ', +sp的类型: ' + typeof +sp + '<hr />');

			document.write('一元加法把空数组([]和new Array())转为数字0: <br />');
			document.write('aa = ' + aa + ', +aa = ' + +aa + ', aa的类型: ' + typeof aa + ', +aa的类型: ' + typeof +aa + '<br />');
			document.write('ac = ' + ac + ', +ac = ' + +ac + ', ac的类型: ' + typeof ac + ', +ac的类型: ' + typeof +ac + '<hr />');

			document.write('一元加法把true、new Boolean(true)转为数字1, false、new Boolean(false)转为数字0: <br />');
			document.write('ba = ' + ba + ', +ba = ' + +ba + ', ba的类型: ' + typeof ba + ', +ba的类型: ' + typeof +ba + '<br />');
			document.write('bb = ' + bb + ', +bb = ' + +bb + ', bb的类型: ' + typeof bb + ', +bb的类型: ' + typeof +bb + '<br />');
			document.write('bc = ' + bc + ', +bc = ' + +bc + ', bc的类型: ' + typeof bc + ', +bc的类型: ' + typeof +bc + '<br />');
			document.write('bd = ' + bd + ', +bd = ' + +bd + ', bd的类型: ' + typeof bd + ', +bd的类型: ' + typeof +bd + '<hr />');
			
			document.write('一元加法把可完整转为数字的字符串转为数字; 八进制的字符串忽略前导0, 然后按十进制转换: <br />');
			document.write('sd = ' + sd + ', +sd = ' + +sd + ', sd的类型: ' + typeof sd + ', +sd的类型: ' + typeof +sd + '<br />');
			document.write('se = ' + se + ', +se = ' + +se + ', se的类型: ' + typeof se + ', +se的类型: ' + typeof +se + '<br />');
			document.write('sg = ' + sg + ', +sg = ' + +sg + ', sg的类型: ' + typeof sg + ', +sg的类型: ' + typeof +sg + '<br />');
			document.write('sh = ' + sh + ', +sh = ' + +sh + ', sh的类型: ' + typeof sh + ', +sh的类型: ' + typeof +sh + '<br />');
			document.write('si = ' + si + ', +si = ' + +si + ', si的类型: ' + typeof si + ', +si的类型: ' + typeof +si + '<br />');
			document.write('sj = ' + sj + ', +sj = ' + +sj + ', sj的类型: ' + typeof sj + ', +sj的类型: ' + typeof +sj + '<br />');
			document.write('sk = ' + sk + ', +sk = ' + +sk + ', sk的类型: ' + typeof sk + ', +sk的类型: ' + typeof +sk + '<br />');
			document.write('sl = ' + sl + ', +sl = ' + +sl + ', sl的类型: ' + typeof sl + ', +sl的类型: ' + typeof +sl + '<br />');
			document.write('sm = ' + sm + ', +sm = ' + +sm + ', sm的类型: ' + typeof sm + ', +sm的类型: ' + typeof +sm + '<br />');
			document.write('sn = ' + sn + ', +sn = ' + +sn + ', sn的类型: ' + typeof sn + ', +sn的类型: ' + typeof +sn + '<br />');
			document.write('so = ' + so + ', +so = ' + +so + ', so的类型: ' + typeof so + ', +so的类型: ' + typeof +so + '<br />');
			document.write('sq = ' + sq + ', +sq = ' + +sq + ', sq的类型: ' + typeof sq + ', +sq的类型: ' + typeof +sq + '<hr />');

			document.write('一元加法把undefined、不能完整转为数字的字符串、对象(不包括空字符串对象和空数组对象)转为数字NaN: <br />');
			document.write('ua = ' + ua + ', +ua = ' + +ua + ', ua的类型: ' + typeof ua + ', +ua的类型: ' + typeof +ua + '<br />');
			document.write('sb = ' + sb + ', +sb = ' + +sb + ', sb的类型: ' + typeof sb + ', +sb的类型: ' + typeof +sb + '<br />');
			document.write('sc = ' + sc + ', +sc = ' + +sc + ', sc的类型: ' + typeof sc + ', +sc的类型: ' + typeof +sc + '<br />');
			document.write('sf = ' + sf + ', +sf = ' + +sf + ', sf的类型: ' + typeof sf + ', +sf的类型: ' + typeof +sf + '<br />');
			document.write('ab = ' + ab + ', +ab = ' + +ab + ', ab的类型: ' + typeof ab + ', +ab的类型: ' + typeof +ab + '<br />');
			document.write('oa = ' + oa + ', +oa = ' + +oa + ', oa的类型: ' + typeof oa + ', +oa的类型: ' + typeof +oa + '<br />');
			document.write('ob = ' + ob + ', +ob = ' + +ob + ', ob的类型: ' + typeof ob + ', +ob的类型: ' + typeof +ob + '<br />');
			document.write('oc = ' + oc + ', +oc = ' + +oc + ', oc的类型: ' + typeof oc + ', +oc的类型: ' + typeof +oc + '<br />');
		</script>
	</body>
</html>

2.9.2. 效果图

3. 一元减法(-)

3.1. 一元减法对数字求负; 数字对象转为原始数字并求负; NaN还是NaN。

3.2. 一元减法把null转为数字0。

3.3. 一元减法把空字符串(''和new String())在数学运算中转为数字0。

3.4. 一元减法把空数组([]和new Array())转为数字0。

3.5. 一元减法把true、new Boolean(true)转为数字-1, false、new Boolean(false)转为数字0。

3.6. 一元减法把可完整转为数字的字符串转为数字并求负; 八进制的字符串忽略前导0, 然后按十进制转换并求负; 字符串NaN转为数字NaN。

3.7. 一元减法把undefined、不能完整转为数字的字符串、对象(不包括空字符串对象(new String())、可完整转为数字的字符串对象(new String('8'))、空数组对象([]和new Array())、布尔对象(new Boolean(true)和new Boolean(false))、数字对象(new Number()构造函数创建的数字对象))转为数字NaN。

3.8. 一元减法并没有改变运算数本身的类型。

3.9. 例

3.9.1. 代码

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<title>一元减法</title>
	</head>
	<body>
		<script type="text/javascript">
			var ua = undefined;
			var la = null;
			var ba = true, bb = false, bc = new Boolean(true), bd = new Boolean(false);
			var na = 0, nb = 1, nc = -10, nd = 0x10, ne = 010, nf = +10, ng = 3.1415926, nh = 5e-324, ni = 1.7976931348623157e+308;
			var nj = NaN, nk = Infinity, nl = -Infinity, nm = new Number(1000);
			var sa = '', sb = '1abc', sc = 'ABC1', sd ='16', se = '3.1415926', sf = '3.14.15926', sg = '+10', sh = '-10', si = '0x10', sj = '010', sk = '5e-324';
			var sl = '1.7976931348623157e+308', sm = 'Infinity', sn = '-Infinity', so = 'NaN', sp = new String(), sq = new String('8');
			var aa = [], ab = ['高性能JavaScript'], ac = new Array();
			var oa = {}, ob = {id: 1001, name: 'zhangsan'}, oc = new Object();

			document.write('一元减法对数字求负; 数字对象转为原始数字并求负; NaN不求负: <br />');
			document.write('na = ' + na + ', -na = ' + -na + ', na的类型: ' + typeof na + ', -na的类型: ' + typeof -na + '<br />');
			document.write('nb = ' + nb + ', -nb = ' + -nb + ', nb的类型: ' + typeof nb + ', -nb的类型: ' + typeof -nb + '<br />');
			document.write('nc = ' + nc + ', -nc = ' + -nc + ', nc的类型: ' + typeof nc + ', -nc的类型: ' + typeof -nc + '<br />');
			document.write('nd = ' + nd + ', -nd = ' + -nd + ', nd的类型: ' + typeof nd + ', -nd的类型: ' + typeof -nd + '<br />');
			document.write('ne = ' + ne + ', -ne = ' + -ne + ', ne的类型: ' + typeof ne + ', -ne的类型: ' + typeof -ne + '<br />');
			document.write('nf = ' + nf + ', -nf = ' + -nf + ', nf的类型: ' + typeof nf + ', -nf的类型: ' + typeof -nf + '<br />');
			document.write('ng = ' + ng + ', -ng = ' + -ng + ', ng的类型: ' + typeof ng + ', -ng的类型: ' + typeof -ng + '<br />');
			document.write('nh = ' + nh + ', -nh = ' + -nh + ', nh的类型: ' + typeof nh + ', -nh的类型: ' + typeof -nh + '<br />');
			document.write('ni = ' + ni + ', -ni = ' + -ni + ', ni的类型: ' + typeof ni + ', -ni的类型: ' + typeof -ni + '<br />');
			document.write('nj = ' + nj + ', -nj = ' + -nj + ', nj的类型: ' + typeof nj + ', -nj的类型: ' + typeof -nj + '<br />');
			document.write('nk = ' + nk + ', -nk = ' + -nk + ', nk的类型: ' + typeof nk + ', -nk的类型: ' + typeof -nk + '<br />');
			document.write('nl = ' + nl + ', -nl = ' + -nl + ', nl的类型: ' + typeof nl + ', -nl的类型: ' + typeof -nl + '<br />');
			document.write('nm = ' + nm + ', -nm = ' + -nm + ', nm的类型: ' + typeof nm + ', -nm的类型: ' + typeof -nm + '<hr />');

			document.write('一元减法把null转为数字0: <br />');
			document.write('la = ' + la + ', -la = ' + -la + ', na的类型: ' + typeof la + ', -na的类型: ' + typeof -la + '<hr />');

			document.write('一元减法把空字符串(\'\'和new String())转为数字0: <br />');
			document.write('sa = ' + sa + ', -sa = ' + -sa + ', sa的类型: ' + typeof sa + ', -sa的类型: ' + typeof -sa + '<br />');
			document.write('sp = ' + sp + ', -sp = ' + -sp + ', sp的类型: ' + typeof sp + ', -sp的类型: ' + typeof -sp + '<hr />');

			document.write('一元减法把空数组([]和new Array())转为数字0: <br />');
			document.write('aa = ' + aa + ', -aa = ' + -aa + ', aa的类型: ' + typeof aa + ', -aa的类型: ' + typeof -aa + '<br />');
			document.write('ac = ' + ac + ', -ac = ' + -ac + ', ac的类型: ' + typeof ac + ', -ac的类型: ' + typeof -ac + '<hr />');

			document.write('一元减法把true、new Boolean(true)转为数字1, false、new Boolean(false)转为数字0: <br />');
			document.write('ba = ' + ba + ', -ba = ' + -ba + ', ba的类型: ' + typeof ba + ', -ba的类型: ' + typeof -ba + '<br />');
			document.write('bb = ' + bb + ', -bb = ' + -bb + ', bb的类型: ' + typeof bb + ', -bb的类型: ' + typeof -bb + '<br />');
			document.write('bc = ' + bc + ', -bc = ' + -bc + ', bc的类型: ' + typeof bc + ', -bc的类型: ' + typeof -bc + '<br />');
			document.write('bd = ' + bd + ', -bd = ' + -bd + ', bd的类型: ' + typeof bd + ', -bd的类型: ' + typeof -bd + '<hr />');
			
			document.write('一元减法把可完整转为数字的字符串转为数字并求负; 八进制的字符串忽略前导0, 然后按十进制转换并求负; NaN只转数字不求负: <br />');
			document.write('sd = ' + sd + ', -sd = ' + -sd + ', sd的类型: ' + typeof sd + ', -sd的类型: ' + typeof -sd + '<br />');
			document.write('se = ' + se + ', -se = ' + -se + ', se的类型: ' + typeof se + ', -se的类型: ' + typeof -se + '<br />');
			document.write('sg = ' + sg + ', -sg = ' + -sg + ', sg的类型: ' + typeof sg + ', -sg的类型: ' + typeof -sg + '<br />');
			document.write('sh = ' + sh + ', -sh = ' + -sh + ', sh的类型: ' + typeof sh + ', -sh的类型: ' + typeof -sh + '<br />');
			document.write('si = ' + si + ', -si = ' + -si + ', si的类型: ' + typeof si + ', -si的类型: ' + typeof -si + '<br />');
			document.write('sj = ' + sj + ', -sj = ' + -sj + ', sj的类型: ' + typeof sj + ', -sj的类型: ' + typeof -sj + '<br />');
			document.write('sk = ' + sk + ', -sk = ' + -sk + ', sk的类型: ' + typeof sk + ', -sk的类型: ' + typeof -sk + '<br />');
			document.write('sl = ' + sl + ', -sl = ' + -sl + ', sl的类型: ' + typeof sl + ', -sl的类型: ' + typeof -sl + '<br />');
			document.write('sm = ' + sm + ', -sm = ' + -sm + ', sm的类型: ' + typeof sm + ', -sm的类型: ' + typeof -sm + '<br />');
			document.write('sn = ' + sn + ', -sn = ' + -sn + ', sn的类型: ' + typeof sn + ', -sn的类型: ' + typeof -sn + '<br />');
			document.write('so = ' + so + ', -so = ' + -so + ', so的类型: ' + typeof so + ', -so的类型: ' + typeof -so + '<br />');
			document.write('sq = ' + sq + ', -sq = ' + -sq + ', sq的类型: ' + typeof sq + ', -sq的类型: ' + typeof -sq + '<hr />');

			document.write('一元减法把undefined、不能完整转为数字的字符串、对象(不包括空字符串对象和空数组对象)转为数字NaN: <br />');
			document.write('ua = ' + ua + ', -ua = ' + -ua + ', ua的类型: ' + typeof ua + ', -ua的类型: ' + typeof -ua + '<br />');
			document.write('sb = ' + sb + ', -sb = ' + -sb + ', sb的类型: ' + typeof sb + ', -sb的类型: ' + typeof -sb + '<br />');
			document.write('sc = ' + sc + ', -sc = ' + -sc + ', sc的类型: ' + typeof sc + ', -sc的类型: ' + typeof -sc + '<br />');
			document.write('sf = ' + sf + ', -sf = ' + -sf + ', sf的类型: ' + typeof sf + ', -sf的类型: ' + typeof -sf + '<br />');
			document.write('ab = ' + ab + ', -ab = ' + -ab + ', ab的类型: ' + typeof ab + ', -ab的类型: ' + typeof -ab + '<br />');
			document.write('oa = ' + oa + ', -oa = ' + -oa + ', oa的类型: ' + typeof oa + ', -oa的类型: ' + typeof -oa + '<br />');
			document.write('ob = ' + ob + ', -ob = ' + -ob + ', ob的类型: ' + typeof ob + ', -ob的类型: ' + typeof -ob + '<br />');
			document.write('oc = ' + oc + ', -oc = ' + -oc + ', oc的类型: ' + typeof oc + ', -oc的类型: ' + typeof -oc + '<br />');
		</script>
	</body>
</html>

3.9.2. 效果图

4. 前增量/后增量运算符

4.1. 所谓前增量运算符, 就是数值上加1, 形式是在变量前放两个加号(++)。

4.2. 后增量运算符也是给数值上加1, 形式是在变量后放两个加号(++)。

4.3. 前增量发生在计算表达式之前; 后增量是在计算过包含它们的表达式后才进行增量。

4.4. 前增量或后增量对数字加1; 数字对象转为原始数字并加1; 对NaN、Infinity和-Infinity无影响。

4.5. 前增量或后增量把null转为数字0并加1。

4.6. 前增量或后增量把空字符串(''和new String())转为数字0并加1。

4.7. 前增量或后增量把空数组([]和new Array())转为数字0并加1。

4.8. 前增量或后增量把true、new Boolean(true)转为数字1并加1, false、new Boolean(false)转为数字0并加1。

4.9. 前增量或后增量把可完整转为数字的字符串转为数字并加1; 八进制的字符串忽略前导0, 然后按十进制转换并加1; 字符串NaN转为数字NaN。

4.10. 前增量或后增量把undefined、不能完整转为数字的字符串、对象(不包括空字符串对象(new String())、可完整转为数字的字符串对象(new String('8'))、空数组对象([]和new Array())、布尔对象(new Boolean(true)和new Boolean(false))、数字对象(new Number()构造函数创建的数字对象))转为数字NaN。

4.11. 增量运算符把运算数本身的类型都改成了数字类型。

4.12. 前增量

4.12.1. 代码

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<title>前增量运算符</title>
	</head>
	<body>
		<script type="text/javascript">
			var ua = undefined;
			var la = null;
			var ba = true, bb = false, bc = new Boolean(true), bd = new Boolean(false);
			var na = 0, nb = 1, nc = -10, nd = 0x10, ne = 010, nf = +10, ng = 3.1415926, nh = 5e-324, ni = 1.7976931348623157e+308;
			var nj = NaN, nk = Infinity, nl = -Infinity, nm = new Number(1000);
			var sa = '', sb = '1abc', sc = 'ABC1', sd ='16', se = '3.1415926', sf = '3.14.15926', sg = '+10', sh = '-10', si = '0x10', sj = '010', sk = '5e-324';
			var sl = '1.7976931348623157e+308', sm = 'Infinity', sn = '-Infinity', so = 'NaN', sp = new String(), sq = new String('8');
			var aa = [], ab = ['高性能JavaScript'], ac = new Array();
			var oa = {}, ob = {id: 1001, name: 'zhangsan'}, oc = new Object();

			document.write('前增量对数字加1; 数字对象转为原始数字并加1; 对NaN、Infinity和-Infinity无影响: <br />');
			document.write('na = ' + na + ', na的类型: ' + typeof na + ', ++na = ' + ++na + '<br />');
			document.write('nb = ' + nb + ', nb的类型: ' + typeof nb + ', ++nb = ' + ++nb + '<br />');
			document.write('nc = ' + nc + ', nc的类型: ' + typeof nc + ', ++nc = ' + ++nc + '<br />');
			document.write('nd = ' + nd + ', nd的类型: ' + typeof nd + ', ++nd = ' + ++nd + '<br />');
			document.write('ne = ' + ne + ', ne的类型: ' + typeof ne + ', ++ne = ' + ++ne + '<br />');
			document.write('nf = ' + nf + ', nf的类型: ' + typeof nf + ', ++nf = ' + ++nf + '<br />');
			document.write('ng = ' + ng + ', ng的类型: ' + typeof ng + ', ++ng = ' + ++ng+ '<br />');
			document.write('nh = ' + nh + ', nh的类型: ' + typeof nh + ', ++nh = ' + ++nh + '<br />');
			document.write('ni = ' + ni + ', ni的类型: ' + typeof ni + ', ++ni = ' + ++ni + '<br />');
			document.write('nj = ' + nj + ', nj的类型: ' + typeof nj + ', ++nj = ' + ++nj + '<br />');
			document.write('nk = ' + nk + ', nk的类型: ' + typeof nk + ', ++nk = ' + ++nk + '<br />');
			document.write('nl = ' + nl + ', nl的类型: ' + typeof nl + ', ++nl = ' + ++nl + '<br />');
			document.write('nm = ' + nm + ', nm的类型: ' + typeof nm + ', ++nm = ' + ++nm + ', 前增量修改了nm本身的类型: ' + typeof nm + '<hr />');

			document.write('前增量把null转为数字0并加1: <br />');
			document.write('la = ' + la + ', na的类型: ' + typeof la + ', ++la = ' + ++la + ', 前增量修改了na本身的类型: ' + typeof la + '<hr />');

			document.write('前增量把空字符串(\'\'和new String())转为数字0并加1: <br />');
			document.write('sa = ' + sa + ', sa的类型: ' + typeof sa + ', ++sa = ' + ++sa + ', 前增量修改了sa本身的类型: ' + typeof sa + '<br />');
			document.write('sp = ' + sp + ', sp的类型: ' + typeof sp + ', ++sp = ' + ++sp + ', 前增量修改了sp本身的类型: ' + typeof sp + '<hr />');

			document.write('前增量把空数组([]和new Array())转为数字0并加1: <br />');
			document.write('aa = ' + aa + ', aa的类型: ' + typeof aa + ', ++aa = ' + ++aa + ', 前增量修改了aa本身的类型: ' + typeof aa + '<br />');
			document.write('ac = ' + ac + ', ac的类型: ' + typeof ac + ', ++ac = ' + ++ac + ', 前增量修改了ac本身的类型: ' + typeof ac + '<hr />');

			document.write('前增量把true、new Boolean(true)转为数字1并加1, false、new Boolean(false)转为数字0并加1: <br />');
			document.write('ba = ' + ba + ', ba的类型: ' + typeof ba + ', ++ba = ' + ++ba + ', 前增量修改了ba本身的类型: ' + typeof ba + '<br />');
			document.write('bb = ' + bb + ', bb的类型: ' + typeof bb + ', ++bb = ' + ++bb + ', 前增量修改了bb本身的类型: ' + typeof bb + '<br />');
			document.write('bc = ' + bc + ', bc的类型: ' + typeof bc + ', ++bc = ' + ++bc + ', 前增量修改了bc本身的类型: ' + typeof bc + '<br />');
			document.write('bd = ' + bd + ', bd的类型: ' + typeof bd + ', ++bd = ' + ++bd + ', 前增量修改了bd本身的类型: ' + typeof bd + '<hr />');
			
			document.write('前增量把可完整转为数字的字符串转为数字并加1; 八进制的字符串忽略前导0, 然后按十进制转换并加1: <br />');
			document.write('sd = ' + sd + ', sd的类型: ' + typeof sd + ', ++sd = ' + ++sd + ', 前增量修改了sd本身的类型: ' + typeof sd + '<br />');
			document.write('se = ' + se + ', se的类型: ' + typeof se + ', ++se = ' + ++se + ', 前增量修改了se本身的类型: ' + typeof se + '<br />');
			document.write('sg = ' + sg + ', sg的类型: ' + typeof sg + ', ++sg = ' + ++sg + ', 前增量修改了sg本身的类型: ' + typeof sg + '<br />');
			document.write('sh = ' + sh + ', sh的类型: ' + typeof sh + ', ++sh = ' + ++sh + ', 前增量修改了sh本身的类型: ' + typeof sh + '<br />');
			document.write('si = ' + si + ', si的类型: ' + typeof si + ', ++si = ' + ++si + ', 前增量修改了si本身的类型: ' + typeof si + '<br />');
			document.write('sj = ' + sj + ', sj的类型: ' + typeof sj + ', ++sj = ' + ++sj + ', 前增量修改了sj本身的类型: ' + typeof sj + '<br />');
			document.write('sk = ' + sk + ', sk的类型: ' + typeof sk + ', ++sk = ' + ++sk + ', 前增量修改了sk本身的类型: ' + typeof sk + '<br />');
			document.write('sl = ' + sl + ', sl的类型: ' + typeof sl + ', ++sl = ' + ++sl + ', 前增量修改了sl本身的类型: ' + typeof sl + '<br />');
			document.write('sm = ' + sm + ', sm的类型: ' + typeof sm + ', ++sm = ' + ++sm + ', 前增量修改了sm本身的类型: ' + typeof sm + '<br />');
			document.write('sn = ' + sn + ', sn的类型: ' + typeof sn + ', ++sn = ' + ++sn + ', 前增量修改了sn本身的类型: ' + typeof sn + '<br />');
			document.write('so = ' + so + ', so的类型: ' + typeof so + ', ++so = ' + ++so + ', 前增量修改了so本身的类型: ' + typeof so + '<br />');
			document.write('sq = ' + sq + ', sq的类型: ' + typeof sq + ', ++sq = ' + ++sq + ', 前增量修改了sq本身的类型: ' + typeof sq + '<hr />');

			document.write('前增量把undefined、不能完整转为数字的字符串、对象(不包括空字符串对象和空数组对象)转为数字NaN: <br />');
			document.write('ua = ' + ua + ', ua的类型: ' + typeof ua + ', ++ua = ' + ++ua + ', 前增量修改了ua本身的类型: ' + typeof ua + '<br />');
			document.write('sb = ' + sb + ', sb的类型: ' + typeof sb + ', ++sb = ' + ++sb + ', 前增量修改了sb本身的类型: ' + typeof sb + '<br />');
			document.write('sc = ' + sc + ', sc的类型: ' + typeof sc + ', ++sc = ' + ++sc + ', 前增量修改了sc本身的类型: ' + typeof sc + '<br />');
			document.write('sf = ' + sf + ', sf的类型: ' + typeof sf + ', ++sf = ' + ++sf + ', 前增量修改了sf本身的类型: ' + typeof sf + '<br />');
			document.write('ab = ' + ab + ', ab的类型: ' + typeof ab + ', ++ab = ' + ++ab + ', 前增量修改了ab本身的类型: ' + typeof ab + '<br />');
			document.write('oa = ' + oa + ', oa的类型: ' + typeof oa + ', ++oa = ' + ++oa + ', 前增量修改了oa本身的类型: ' + typeof oa + '<br />');
			document.write('ob = ' + ob + ', ob的类型: ' + typeof ob + ', ++ob = ' + ++ob + ', 前增量修改了ob本身的类型: ' + typeof ob + '<br />');
			document.write('oc = ' + oc + ', oc的类型: ' + typeof oc + ', ++oc = ' + ++oc + ', 前增量修改了oc本身的类型: ' + typeof oc + '<br />');
		</script>
	</body>
</html>

4.12.2. 效果图

4.13. 后增量

4.13.1. 代码

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<title>后增量运算符</title>
	</head>
	<body>
		<script type="text/javascript">
			var ua = undefined;
			var la = null;
			var ba = true, bb = false, bc = new Boolean(true), bd = new Boolean(false);
			var na = 0, nb = 1, nc = -10, nd = 0x10, ne = 010, nf = +10, ng = 3.1415926, nh = 5e-324, ni = 1.7976931348623157e+308;
			var nj = NaN, nk = Infinity, nl = -Infinity, nm = new Number(1000);
			var sa = '', sb = '1abc', sc = 'ABC1', sd ='16', se = '3.1415926', sf = '3.14.15926', sg = '+10', sh = '-10', si = '0x10', sj = '010', sk = '5e-324';
			var sl = '1.7976931348623157e+308', sm = 'Infinity', sn = '-Infinity', so = 'NaN', sp = new String(), sq = new String('8');
			var aa = [], ab = ['高性能JavaScript'], ac = new Array();
			var oa = {}, ob = {id: 1001, name: 'zhangsan'}, oc = new Object();

			document.write('后增量对数字加1; 数字对象转为原始数字并加1; 对NaN、Infinity和-Infinity无影响: <br />');
			document.write('na = ' + na + ', na的类型: ' + typeof na + ', na++ = ' + na++ + ', na = ' + na + '<br />');
			document.write('nb = ' + nb + ', nb的类型: ' + typeof nb + ', nb++ = ' + nb++ + ', nb = ' + nb + '<br />');
			document.write('nc = ' + nc + ', nc的类型: ' + typeof nc + ', nc++ = ' + nc++ + ', nc = ' + nc + '<br />');
			document.write('nd = ' + nd + ', nd的类型: ' + typeof nd + ', nd++ = ' + nd++ + ', nd = ' + nd + '<br />');
			document.write('ne = ' + ne + ', ne的类型: ' + typeof ne + ', ne++ = ' + ne++ + ', ne = ' + ne + '<br />');
			document.write('nf = ' + nf + ', nf的类型: ' + typeof nf + ', nf++ = ' + nf++ + ', nf = ' + nf + '<br />');
			document.write('ng = ' + ng + ', ng的类型: ' + typeof ng + ', ng++ = ' + ng++ + ', ng = ' + ng + '<br />');
			document.write('nh = ' + nh + ', nh的类型: ' + typeof nh + ', nh++ = ' + nh++ + ', nh = ' + nh + '<br />');
			document.write('ni = ' + ni + ', ni的类型: ' + typeof ni + ', ni++ = ' + ni++ + ', ni = ' + ni + '<br />');
			document.write('nj = ' + nj + ', nj的类型: ' + typeof nj + ', nj++ = ' + nj++ + ', nj = ' + nj + '<br />');
			document.write('nk = ' + nk + ', nk的类型: ' + typeof nk + ', nk++ = ' + nk++ + ', nk = ' + nk + '<br />');
			document.write('nl = ' + nl + ', nl的类型: ' + typeof nl + ', nl++ = ' + nl++ + ', nl = ' + nl + '<br />');
			document.write('nm = ' + nm + ', nm的类型: ' + typeof nm + ', nm++ = ' + nm++ + ', 后增量修改了nm本身的类型: ' + typeof nm + ', nm =  ' + nm + '<hr />');

			document.write('后增量把null转为数字0并加1: <br />');
			document.write('la = ' + la + ', na的类型: ' + typeof la + ', la++ = ' + la++ + ', 后增量修改了na本身的类型: ' + typeof la + ', la =  ' + la + '<hr />');
			
			document.write('后增量把空字符串(\'\'和new String())转为数字0并加1: <br />');
			document.write('sa = ' + sa + ', sa的类型: ' + typeof sa + ', sa++ = ' + sa++ + ', 后增量修改了sa本身的类型: ' + typeof sa + ', sa =  ' + sa + '<br />');
			document.write('sp = ' + sp + ', sp的类型: ' + typeof sp + ', sp++ = ' + sp++ + ', 后增量修改了sp本身的类型: ' + typeof sp + ', sp =  ' + sp + '<hr />');

			document.write('后增量把空数组([]和new Array())转为数字0并加1: <br />');
			document.write('aa = ' + aa + ', aa的类型: ' + typeof aa + ', aa++ = ' + aa++ + ', 后增量修改了aa本身的类型: ' + typeof aa + ', aa =  ' + aa + '<br />');
			document.write('ac = ' + ac + ', ac的类型: ' + typeof ac + ', ac++ = ' + ac++ + ', 后增量修改了ac本身的类型: ' + typeof ac + ', ac =  ' + ac + '<hr />');

			document.write('后增量把true、new Boolean(true)转为数字1并加1, false、new Boolean(false)转为数字0并加1: <br />');
			document.write('ba = ' + ba + ', ba的类型: ' + typeof ba + ', ba++ = ' + ba++ + ', 后增量修改了ba本身的类型: ' + typeof ba + ', ba =  ' + ba + '<br />');
			document.write('bb = ' + bb + ', bb的类型: ' + typeof bb + ', bb++ = ' + bb++ + ', 后增量修改了bb本身的类型: ' + typeof bb + ', bb =  ' + bb + '<br />');
			document.write('bc = ' + bc + ', bc的类型: ' + typeof bc + ', bc++ = ' + bc++ + ', 后增量修改了bc本身的类型: ' + typeof bc + ', bc =  ' + bc + '<br />');
			document.write('bd = ' + bd + ', bd的类型: ' + typeof bd + ', bd++ = ' + bd++ + ', 后增量修改了bd本身的类型: ' + typeof bd + ', bd =  ' + bd + '<hr />');
			
			document.write('后增量把可完整转为数字的字符串转为数字并加1; 八进制的字符串忽略前导0, 然后按十进制转换并加1: <br />');
			document.write('sd = ' + sd + ', sd的类型: ' + typeof sd + ', sd++ = ' + sd++ + ', 后增量修改了sd本身的类型: ' + typeof sd + ', sd =  ' + sd + '<br />');
			document.write('se = ' + se + ', se的类型: ' + typeof se + ', se++ = ' + se++ + ', 后增量修改了se本身的类型: ' + typeof se + ', se =  ' + se + '<br />');
			document.write('sg = ' + sg + ', sg的类型: ' + typeof sg + ', sg++ = ' + sg++ + ', 后增量修改了sg本身的类型: ' + typeof sg + ', sg =  ' + sg + '<br />');
			document.write('sh = ' + sh + ', sh的类型: ' + typeof sh + ', sh++ = ' + sh++ + ', 后增量修改了sh本身的类型: ' + typeof sh + ', sh =  ' + sh + '<br />');
			document.write('si = ' + si + ', si的类型: ' + typeof si + ', si++ = ' + si++ + ', 后增量修改了si本身的类型: ' + typeof si + ', si =  ' + si + '<br />');
			document.write('sj = ' + sj + ', sj的类型: ' + typeof sj + ', sj++ = ' + sj++ + ', 后增量修改了sj本身的类型: ' + typeof sj + ', sj =  ' + sj + '<br />');
			document.write('sk = ' + sk + ', sk的类型: ' + typeof sk + ', sk++ = ' + sk++ + ', 后增量修改了sk本身的类型: ' + typeof sk + ', sk =  ' + sk + '<br />');
			document.write('sl = ' + sl + ', sl的类型: ' + typeof sl + ', sl++ = ' + sl++ + ', 后增量修改了sl本身的类型: ' + typeof sl + ', sl =  ' + sl + '<br />');
			document.write('sm = ' + sm + ', sm的类型: ' + typeof sm + ', sm++ = ' + sm++ + ', 后增量修改了sm本身的类型: ' + typeof sm + ', sm =  ' + sm + '<br />');
			document.write('sn = ' + sn + ', sn的类型: ' + typeof sn + ', sn++ = ' + sn++ + ', 后增量修改了sn本身的类型: ' + typeof sn + ', sn =  ' + sn + '<br />');
			document.write('so = ' + so + ', so的类型: ' + typeof so + ', so++ = ' + so++ + ', 后增量修改了so本身的类型: ' + typeof so + ', so =  ' + so + '<br />');
			document.write('sq = ' + sq + ', sq的类型: ' + typeof sq + ', sq++ = ' + sq++ + ', 后增量修改了sq本身的类型: ' + typeof sq + ', sq =  ' + sq + '<hr />');

			document.write('后增量把undefined、不能完整转为数字的字符串、对象(不包括空字符串对象和空数组对象)转为数字NaN: <br />');
			document.write('ua = ' + ua + ', ua的类型: ' + typeof ua + ', ua++ = ' + ua++ + ', 后增量修改了ua本身的类型: ' + typeof ua + '<br />');
			document.write('sb = ' + sb + ', sb的类型: ' + typeof sb + ', sb++ = ' + sb++ + ', 后增量修改了sb本身的类型: ' + typeof sb + '<br />');
			document.write('sc = ' + sc + ', sc的类型: ' + typeof sc + ', sc++ = ' + sc++ + ', 后增量修改了sc本身的类型: ' + typeof sc + '<br />');
			document.write('sf = ' + sf + ', sf的类型: ' + typeof sf + ', sf++ = ' + sf++ + ', 后增量修改了sf本身的类型: ' + typeof sf + '<br />');
			document.write('ab = ' + ab + ', ab的类型: ' + typeof ab + ', ab++ = ' + ab++ + ', 后增量修改了ab本身的类型: ' + typeof ab + '<br />');
			document.write('oa = ' + oa + ', oa的类型: ' + typeof oa + ', oa++ = ' + oa++ + ', 后增量修改了oa本身的类型: ' + typeof oa + '<br />');
			document.write('ob = ' + ob + ', ob的类型: ' + typeof ob + ', ob++ = ' + ob++ + ', 后增量修改了ob本身的类型: ' + typeof ob + '<br />');
			document.write('oc = ' + oc + ', oc的类型: ' + typeof oc + ', oc++ = ' + oc++ + ', 后增量修改了oc本身的类型: ' + typeof oc + '<br />');
		</script>
	</body>
</html>

4.13.2. 效果图

5. 前减量/后减量运算符

5.1. 同样, 前减量运算符是从数值上减1, 形式是在变量前放两个减号(--)。

5.2. 后减量运算符也是从数值上减1, 形式为在变量后加两个减号(--)。

5.3. 前减量发生在计算表达式之前; 后减量是在计算过包含它们的表达式后才进行减量。

5.4. 前减量或后减量对数字减1; 数字对象转为原始数字并减1; 对NaN、Infinity和-Infinity无影响。

5.5. 前减量或后减量把null转为数字0并减1。

5.6. 前减量或后减量把空字符串(''和new String())转为数字0并减1。

5.7. 前减量或后减量把空数组([]和new Array())转为数字0并减1。

5.8. 前减量或后减量把true、new Boolean(true)转为数字1并减1, false、new Boolean(false)转为数字0并减1。

5.9. 前减量或后减量把可完整转为数字的字符串转为数字并减1; 八进制的字符串忽略前导0, 然后按十进制转换并减1; 字符串NaN转为数字NaN。

5.10. 前减量或后减量把undefined、不能完整转为数字的字符串、对象(不包括空字符串对象(new String())、可完整转为数字的字符串对象(new String('8'))、空数组对象([]和new Array())、布尔对象(new Boolean(true)和new Boolean(false))、数字对象(new Number()构造函数创建的数字对象))转为数字NaN。

5.11. 减量运算符把运算数本身的类型都改成了数字类型。

5.12. 减量例

5.12.1. 代码

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<title>前减量运算符</title>
	</head>
	<body>
		<script type="text/javascript">
			var ua = undefined;
			var la = null;
			var ba = true, bb = false, bc = new Boolean(true), bd = new Boolean(false);
			var na = 0, nb = 1, nc = -10, nd = 0x10, ne = 010, nf = +10, ng = 3.1415926, nh = 5e-324, ni = 1.7976931348623157e+308;
			var nj = NaN, nk = Infinity, nl = -Infinity, nm = new Number(1000);
			var sa = '', sb = '1abc', sc = 'ABC1', sd ='16', se = '3.1415926', sf = '3.14.15926', sg = '+10', sh = '-10', si = '0x10', sj = '010', sk = '5e-324';
			var sl = '1.7976931348623157e+308', sm = 'Infinity', sn = '-Infinity', so = 'NaN', sp = new String(), sq = new String('8');
			var aa = [], ab = ['高性能JavaScript'], ac = new Array();
			var oa = {}, ob = {id: 1001, name: 'zhangsan'}, oc = new Object();

			document.write('前减量对数字减1; 数字对象转为原始数字并减1; 对NaN、Infinity和-Infinity无影响: <br />');
			document.write('na = ' + na + ', na的类型: ' + typeof na + ', --na = ' + --na + '<br />');
			document.write('nb = ' + nb + ', nb的类型: ' + typeof nb + ', --nb = ' + --nb + '<br />');
			document.write('nc = ' + nc + ', nc的类型: ' + typeof nc + ', --nc = ' + --nc + '<br />');
			document.write('nd = ' + nd + ', nd的类型: ' + typeof nd + ', --nd = ' + --nd + '<br />');
			document.write('ne = ' + ne + ', ne的类型: ' + typeof ne + ', --ne = ' + --ne + '<br />');
			document.write('nf = ' + nf + ', nf的类型: ' + typeof nf + ', --nf = ' + --nf + '<br />');
			document.write('ng = ' + ng + ', ng的类型: ' + typeof ng + ', --ng = ' + --ng + '<br />');
			document.write('nh = ' + nh + ', nh的类型: ' + typeof nh + ', --nh = ' + --nh + '<br />');
			document.write('ni = ' + ni + ', ni的类型: ' + typeof ni + ', --ni = ' + --ni + '<br />');
			document.write('nj = ' + nj + ', nj的类型: ' + typeof nj + ', --nj = ' + --nj + '<br />');
			document.write('nk = ' + nk + ', nk的类型: ' + typeof nk + ', --nk = ' + --nk + '<br />');
			document.write('nl = ' + nl + ', nl的类型: ' + typeof nl + ', --nl = ' + --nl + '<br />');
			document.write('nm = ' + nm + ', nm的类型: ' + typeof nm + ', --nm = ' + --nm + ', 前减量修改了nm本身的类型: ' + typeof nm + '<hr />');

			document.write('前减量把null转为数字0并减1: <br />');
			document.write('la = ' + la + ', na的类型: ' + typeof la + ', --la = ' + --la + ', 前减量修改了na本身的类型: ' + typeof la + '<hr />');

			document.write('前减量把空字符串(\'\'和new String())转为数字0并减1: <br />');
			document.write('sa = ' + sa + ', sa的类型: ' + typeof sa + ', --sa = ' + --sa + ', 前减量修改了sa本身的类型: ' + typeof sa + '<br />');
			document.write('sp = ' + sp + ', sp的类型: ' + typeof sp + ', --sp = ' + --sp + ', 前减量修改了sp本身的类型: ' + typeof sp + '<hr />');

			document.write('前减量把空数组([]和new Array())转为数字0并减1: <br />');
			document.write('aa = ' + aa + ', aa的类型: ' + typeof aa + ', --aa = ' + --aa + ', 前减量修改了aa本身的类型: ' + typeof aa + '<br />');
			document.write('ac = ' + ac + ', ac的类型: ' + typeof ac + ', --ac = ' + --ac + ', 前减量修改了ac本身的类型: ' + typeof ac + '<hr />');

			document.write('前减量把true、new Boolean(true)转为数字1并减1, false、new Boolean(false)转为数字0并减1: <br />');
			document.write('ba = ' + ba + ', ba的类型: ' + typeof ba + ', --ba = ' + --ba + ', 前减量修改了ba本身的类型: ' + typeof ba + '<br />');
			document.write('bb = ' + bb + ', bb的类型: ' + typeof bb + ', --bb = ' + --bb + ', 前减量修改了bb本身的类型: ' + typeof bb + '<br />');
			document.write('bc = ' + bc + ', bc的类型: ' + typeof bc + ', --bc = ' + --bc + ', 前减量修改了bc本身的类型: ' + typeof bc + '<br />');
			document.write('bd = ' + bd + ', bd的类型: ' + typeof bd + ', --bd = ' + --bd + ', 前减量修改了bd本身的类型: ' + typeof bd + '<hr />');

			document.write('前减量把可完整转为数字的字符串转为数字并减1; 八进制的字符串忽略前导0, 然后按十进制转换并减1: <br />');
			document.write('sd = ' + sd + ', sd的类型: ' + typeof sd + ', --sd = ' + --sd + ', 前减量修改了sd本身的类型: ' + typeof sd + '<br />');
			document.write('se = ' + se + ', se的类型: ' + typeof se + ', --se = ' + --se + ', 前减量修改了se本身的类型: ' + typeof se + '<br />');
			document.write('sg = ' + sg + ', sg的类型: ' + typeof sg + ', --sg = ' + --sg + ', 前减量修改了sg本身的类型: ' + typeof sg + '<br />');
			document.write('sh = ' + sh + ', sh的类型: ' + typeof sh + ', --sh = ' + --sh + ', 前减量修改了sh本身的类型: ' + typeof sh + '<br />');
			document.write('si = ' + si + ', si的类型: ' + typeof si + ', --si = ' + --si + ', 前减量修改了si本身的类型: ' + typeof si + '<br />');
			document.write('sj = ' + sj + ', sj的类型: ' + typeof sj + ', --sj = ' + --sj + ', 前减量修改了sj本身的类型: ' + typeof sj + '<br />');
			document.write('sk = ' + sk + ', sk的类型: ' + typeof sk + ', --sk = ' + --sk + ', 前减量修改了sk本身的类型: ' + typeof sk + '<br />');
			document.write('sl = ' + sl + ', sl的类型: ' + typeof sl + ', --sl = ' + --sl + ', 前减量修改了sl本身的类型: ' + typeof sl + '<br />');
			document.write('sm = ' + sm + ', sm的类型: ' + typeof sm + ', --sm = ' + --sm + ', 前减量修改了sm本身的类型: ' + typeof sm + '<br />');
			document.write('sn = ' + sn + ', sn的类型: ' + typeof sn + ', --sn = ' + --sn + ', 前减量修改了sn本身的类型: ' + typeof sn + '<br />');
			document.write('so = ' + so + ', so的类型: ' + typeof so + ', --so = ' + --so + ', 前减量修改了so本身的类型: ' + typeof so + '<br />');
			document.write('sq = ' + sq + ', sq的类型: ' + typeof sq + ', --sq = ' + --sq + ', 前减量修改了sq本身的类型: ' + typeof sq + '<hr />');

			document.write('前减量把undefined、不能完整转为数字的字符串、对象(不包括空字符串对象和空数组对象)转为数字NaN: <br />');
			document.write('ua = ' + ua + ', ua的类型: ' + typeof ua + ', --ua = ' + --ua + ', 前减量修改了ua本身的类型: ' + typeof ua + '<br />');
			document.write('sb = ' + sb + ', sb的类型: ' + typeof sb + ', --sb = ' + --sb + ', 前减量修改了sb本身的类型: ' + typeof sb + '<br />');
			document.write('sc = ' + sc + ', sc的类型: ' + typeof sc + ', --sc = ' + --sc + ', 前减量修改了sc本身的类型: ' + typeof sc + '<br />');
			document.write('sf = ' + sf + ', sf的类型: ' + typeof sf + ', --sf = ' + --sf + ', 前减量修改了sf本身的类型: ' + typeof sf + '<br />');
			document.write('ab = ' + ab + ', ab的类型: ' + typeof ab + ', --ab = ' + --ab + ', 前减量修改了ab本身的类型: ' + typeof ab + '<br />');
			document.write('oa = ' + oa + ', oa的类型: ' + typeof oa + ', --oa = ' + --oa + ', 前减量修改了oa本身的类型: ' + typeof oa + '<br />');
			document.write('ob = ' + ob + ', ob的类型: ' + typeof ob + ', --ob = ' + --ob + ', 前减量修改了ob本身的类型: ' + typeof ob + '<br />');
			document.write('oc = ' + oc + ', oc的类型: ' + typeof oc + ', --oc = ' + --oc + ', 前减量修改了oc本身的类型: ' + typeof oc + '<br />');
		</script>
	</body>
</html>

5.12.2. 效果图

5.13. 减量例

5.13.1. 代码

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<title>后减量运算符</title>
	</head>
	<body>
		<script type="text/javascript">
			var ua = undefined;
			var la = null;
			var ba = true, bb = false, bc = new Boolean(true), bd = new Boolean(false);
			var na = 0, nb = 1, nc = -10, nd = 0x10, ne = 010, nf = +10, ng = 3.1415926, nh = 5e-324, ni = 1.7976931348623157e+308;
			var nj = NaN, nk = Infinity, nl = -Infinity, nm = new Number(1000);
			var sa = '', sb = '1abc', sc = 'ABC1', sd ='16', se = '3.1415926', sf = '3.14.15926', sg = '+10', sh = '-10', si = '0x10', sj = '010', sk = '5e-324';
			var sl = '1.7976931348623157e+308', sm = 'Infinity', sn = '-Infinity', so = 'NaN', sp = new String(), sq = new String('8');
			var aa = [], ab = ['高性能JavaScript'], ac = new Array();
			var oa = {}, ob = {id: 1001, name: 'zhangsan'}, oc = new Object();

			document.write('后减量对数字减1; 数字对象转为原始数字并减1; 对NaN、Infinity和-Infinity无影响: <br />');
			document.write('na = ' + na + ', na的类型: ' + typeof na + ', na-- = ' + na-- + ', na = ' + na + '<br />');
			document.write('nb = ' + nb + ', nb的类型: ' + typeof nb + ', nb-- = ' + nb-- + ', nb = ' + nb + '<br />');
			document.write('nc = ' + nc + ', nc的类型: ' + typeof nc + ', nc-- = ' + nc-- + ', nc = ' + nc + '<br />');
			document.write('nd = ' + nd + ', nd的类型: ' + typeof nd + ', nd-- = ' + nd-- + ', nd = ' + nd + '<br />');
			document.write('ne = ' + ne + ', ne的类型: ' + typeof ne + ', ne-- = ' + ne-- + ', ne = ' + ne + '<br />');
			document.write('nf = ' + nf + ', nf的类型: ' + typeof nf + ', nf-- = ' + nf-- + ', nf = ' + nf + '<br />');
			document.write('ng = ' + ng + ', ng的类型: ' + typeof ng + ', ng-- = ' + ng-- + ', ng = ' + ng + '<br />');
			document.write('nh = ' + nh + ', nh的类型: ' + typeof nh + ', nh-- = ' + nh-- + ', nh = ' + nh + '<br />');
			document.write('ni = ' + ni + ', ni的类型: ' + typeof ni + ', ni-- = ' + ni-- + ', ni = ' + ni + '<br />');
			document.write('nj = ' + nj + ', nj的类型: ' + typeof nj + ', nj-- = ' + nj-- + ', nj = ' + nj + '<br />');
			document.write('nk = ' + nk + ', nk的类型: ' + typeof nk + ', nk-- = ' + nk-- + ', nk = ' + nk + '<br />');
			document.write('nl = ' + nl + ', nl的类型: ' + typeof nl + ', nl-- = ' + nl-- + ', nl = ' + nl + '<br />');
			document.write('nm = ' + nm + ', nm的类型: ' + typeof nm + ', nm-- = ' + nm-- + ', 后减量修改了nm本身的类型: ' + typeof nm + ', nm =  ' + nm + '<hr />');

			document.write('后减量把null转为数字0并减1: <br />');
			document.write('la = ' + la + ', na的类型: ' + typeof la + ', la-- = ' + la-- + ', 后减量修改了na本身的类型: ' + typeof la + ', la =  ' + la + '<hr />');

			document.write('后减量把空字符串(\'\'和new String())转为数字0并减1: <br />');
			document.write('sa = ' + sa + ', sa的类型: ' + typeof sa + ', sa-- = ' + sa-- + ', 后减量修改了sa本身的类型: ' + typeof sa + ', sa =  ' + sa + '<br />');
			document.write('sp = ' + sp + ', sp的类型: ' + typeof sp + ', sp-- = ' + sp-- + ', 后减量修改了sp本身的类型: ' + typeof sp + ', sp =  ' + sp + '<hr />');

			document.write('后减量把空数组([]和new Array())转为数字0并减1: <br />');
			document.write('aa = ' + aa + ', aa的类型: ' + typeof aa + ', aa-- = ' + aa-- + ', 后减量修改了aa本身的类型: ' + typeof aa + ', aa =  ' + aa + '<br />');
			document.write('ac = ' + ac + ', ac的类型: ' + typeof ac + ', ac-- = ' + ac-- + ', 后减量修改了ac本身的类型: ' + typeof ac + ', ac =  ' + ac + '<hr />');

			document.write('后减量把true、new Boolean(true)转为数字1并减1, false、new Boolean(false)转为数字0并减1: <br />');
			document.write('ba = ' + ba + ', ba的类型: ' + typeof ba + ', ba-- = ' + ba-- + ', 后减量修改了ba本身的类型: ' + typeof ba + ', ba =  ' + ba + '<br />');
			document.write('bb = ' + bb + ', bb的类型: ' + typeof bb + ', bb-- = ' + bb-- + ', 后减量修改了bb本身的类型: ' + typeof bb + ', bb =  ' + bb + '<br />');
			document.write('bc = ' + bc + ', bc的类型: ' + typeof bc + ', bc-- = ' + bc-- + ', 后减量修改了bc本身的类型: ' + typeof bc + ', bc =  ' + bc + '<br />');
			document.write('bd = ' + bd + ', bd的类型: ' + typeof bd + ', bd-- = ' + bd-- + ', 后减量修改了bd本身的类型: ' + typeof bd + ', bd =  ' + bd + '<hr />');
			
			document.write('后减量把可完整转为数字的字符串转为数字并减1; 八进制的字符串忽略前导0, 然后按十进制转换并减1: <br />');
			document.write('sd = ' + sd + ', sd的类型: ' + typeof sd + ', sd-- = ' + sd-- + ', 后减量修改了sd本身的类型: ' + typeof sd + ', sd =  ' + sd + '<br />');
			document.write('se = ' + se + ', se的类型: ' + typeof se + ', se-- = ' + se-- + ', 后减量修改了se本身的类型: ' + typeof se + ', se =  ' + se + '<br />');
			document.write('sg = ' + sg + ', sg的类型: ' + typeof sg + ', sg-- = ' + sg-- + ', 后减量修改了sg本身的类型: ' + typeof sg + ', sg =  ' + sg + '<br />');
			document.write('sh = ' + sh + ', sh的类型: ' + typeof sh + ', sh-- = ' + sh-- + ', 后减量修改了sh本身的类型: ' + typeof sh + ', sh =  ' + sh + '<br />');
			document.write('si = ' + si + ', si的类型: ' + typeof si + ', si-- = ' + si-- + ', 后减量修改了si本身的类型: ' + typeof si + ', si =  ' + si + '<br />');
			document.write('sj = ' + sj + ', sj的类型: ' + typeof sj + ', sj-- = ' + sj-- + ', 后减量修改了sj本身的类型: ' + typeof sj + ', sj =  ' + sj + '<br />');
			document.write('sk = ' + sk + ', sk的类型: ' + typeof sk + ', sk-- = ' + sk-- + ', 后减量修改了sk本身的类型: ' + typeof sk + ', sk =  ' + sk + '<br />');
			document.write('sl = ' + sl + ', sl的类型: ' + typeof sl + ', sl-- = ' + sl-- + ', 后减量修改了sl本身的类型: ' + typeof sl + ', sl =  ' + sl + '<br />');
			document.write('sm = ' + sm + ', sm的类型: ' + typeof sm + ', sm-- = ' + sm-- + ', 后减量修改了sm本身的类型: ' + typeof sm + ', sm =  ' + sm + '<br />');
			document.write('sn = ' + sn + ', sn的类型: ' + typeof sn + ', sn-- = ' + sn-- + ', 后减量修改了sn本身的类型: ' + typeof sn + ', sn =  ' + sn + '<br />');
			document.write('so = ' + so + ', so的类型: ' + typeof so + ', so-- = ' + so-- + ', 后减量修改了so本身的类型: ' + typeof so + ', so =  ' + so + '<br />');
			document.write('sq = ' + sq + ', sq的类型: ' + typeof sq + ', sq-- = ' + sq-- + ', 后减量修改了sq本身的类型: ' + typeof sq + ', sq =  ' + sq + '<hr />');

			document.write('后减量把undefined、不能完整转为数字的字符串、对象(不包括空字符串对象和空数组对象)转为数字NaN: <br />');
			document.write('ua = ' + ua + ', ua的类型: ' + typeof ua + ', ua-- = ' + ua-- + ', 后减量修改了ua本身的类型: ' + typeof ua + '<br />');
			document.write('sb = ' + sb + ', sb的类型: ' + typeof sb + ', sb-- = ' + sb-- + ', 后减量修改了sb本身的类型: ' + typeof sb + '<br />');
			document.write('sc = ' + sc + ', sc的类型: ' + typeof sc + ', sc-- = ' + sc-- + ', 后减量修改了sc本身的类型: ' + typeof sc + '<br />');
			document.write('sf = ' + sf + ', sf的类型: ' + typeof sf + ', sf-- = ' + sf-- + ', 后减量修改了sf本身的类型: ' + typeof sf + '<br />');
			document.write('ab = ' + ab + ', ab的类型: ' + typeof ab + ', ab-- = ' + ab-- + ', 后减量修改了ab本身的类型: ' + typeof ab + '<br />');
			document.write('oa = ' + oa + ', oa的类型: ' + typeof oa + ', oa-- = ' + oa-- + ', 后减量修改了oa本身的类型: ' + typeof oa + '<br />');
			document.write('ob = ' + ob + ', ob的类型: ' + typeof ob + ', ob-- = ' + ob-- + ', 后减量修改了ob本身的类型: ' + typeof ob + '<br />');
			document.write('oc = ' + oc + ', oc的类型: ' + typeof oc + ', oc-- = ' + oc-- + ', 后减量修改了oc本身的类型: ' + typeof oc + '<br />');
		</script>
	</body>
</html>

5.13.2. 效果图

6. void运算符

6.1. void运算符总是返回undefined。该运算符通常用于避免输出不应该输出的值。

6.2. 从html的<a>元素调用JavaScript函数

6.2.1. 从html的<a>元素调用JavaScript函数时, 函数不能返回有效值, 否则浏览器将清空页面, 只显示函数的结果:

<a href="javascript:window.open('about:blank')">Click me</a>

6.2.2. 如果把上面这行代码放入html页面, 点击其中的链接, 即可看到屏幕上显示"[object]"。这是因为window.open()方法返回了新打开的窗口的引用。然后该对象将被转换成要显示的字符串。

6.2.3. 要避免这种效果, 可以用void运算符调用 window.open()函数:

<a href="javascript:void(window.open('about:blank'))">Click me</a>

6.2.4. 这使window.open()调用返回undefined, 它不是有效值, 不会显示在浏览器窗口中。

6.3. 没有返回值的函数真正返回的都是undefined。

6.4. 例

6.4.1. 代码

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<title>void运算符</title>
	</head>
	<body>
		<a href="javascript:window.open('about:blank');">Click me</a> 
		<br /><br />
		<a href="javascript:void(window.open('about:blank'));">Click me</a>
	</body>
</html>

6.4.2. 效果图

7. delete运算符

7.1. delete操作符被设计用于对象属性。它对变量或函数没有影响。  
7.2. delete关键词从对象中删除属性。
7.3. delete关键词会同时删除属性的值和属性本身。删除完成后, 属性在被添加回来之前是无法使用的。
7.4. delete操作符不应被用于预定义的JavaScript对象属性。这样做会使应用程序崩溃。

7.3. 例

7.3.1. 代码

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<title>delete运算符</title>
	</head>
	<body>
		<script type="text/javascript">
			var o = {id: 100001, name: "David"};

			for(var key in o){
				document.write(key + " = " + o[key] + '<br />');
			}
			document.write('------------------------<br />');
			
			delete o.name;
			for(var key in o){
				document.write(key + " = " + o[key] + '<br />');
			}
		</script>
	</body>
</html>

7.3.2. 效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值