原文地址:http://www.cnblogs.com/sanshi/archive/2009/08/30/1556677.html
前段时间我曾经对JavaScript中的应用技巧进行了收集和总结,形成了以下几篇文章:
这里我将会对这些应用技巧进行集中描述,如果你觉得遗漏了一些好用的应用技巧,也请在留言中提出,我会及时更新到这篇文章中的。
1.
0 ==
false
;
// true
2.
1 ==
true
;
// true
3.
''
==
false
// true
4.
null
==
false
// true
1.
0 ===
false
;
// false
2.
1 ===
true
;
// false
3.
''
===
false
// false
4.
null
===
false
// false
1.
!!0 ===
false
;
// true
2.
!!1 ===
true
;
// true
3.
!!
''
===
false
// true
4.
!!
null
===
false
// true
1.
function
plus(base, added) {
2.
return
base + added;
3.
}
4.
plus(2);
// NaN
1.
function
plus(base, added) {
2.
added = added || 1;
3.
return
base + added;
4.
}
5.
plus(2);
// 3
6.
plus(2, 2);
// 4
1.
function
plus(base, added) {
2.
added = added || (added === 0 ? 0 : 1);
3.
return
base + added;
4.
}
1.
if
(top !== window) {
2.
top.location.href = window.location.href;
3.
}
1.
'Hello world, hello world'
.replace(
'world'
,
'JavaScript'
);
2.
// The result is "Hello JavaScript, hello world"
1.
'Hello world, hello world'
.replace(/world/g,
'JavaScript'
);
2.
// The result is "Hello JavaScript, hello JavaScript"
1.
'Hello world, hello world'
.replace(/hello/gi,
'Hi'
);
2.
// The result is "Hi world, Hi world"
1.
function
args() {
2.
return
[].slice.call(arguments, 0);
3.
}
4.
args(2, 5, 8);
// [2, 5, 8]
1.
parseInt(str, [radix])
1.
parseInt(
'08'
);
// 0
2.
parseInt(
'08'
, 10);
// 8
1.
var
arr = [1, 2, 3, 4, 5];
2.
delete
arr[1];
3.
arr;
// [1, undefined, 3, 4, 5]
1.
var
arr = [1, 2, 3, 4, 5];
2.
arr.splice(1, 1);
3.
arr;
// [1, 3, 4, 5]
1.
function
add() {
2.
return
add.count++;
3.
}
4.
add.count = 0;
5.
add();
// 0
6.
add();
// 1
7.
add();
// 2
01.
function
add() {
02.
if
(!arguments.callee.count) {
03.
arguments.callee.count = 0;
04.
}
05.
return
arguments.callee.count++;
06.
}
07.
add();
// 0
08.
add();
// 1
09.
add();
// 2
1.
var
arr = [2, 3, 45, 12, 8];
2.
var
max = arr[0];
3.
for
(
var
i
in
arr) {
4.
if
(arr[i] > max) {
5.
max = arr[i];
6.
}
7.
}
8.
max;
// 45
1.
Math.max(2, 3, 45, 12, 8);
// 45
1.
var
arr = [2, 3, 45, 12, 8];
2.
Math.max.apply(
null
, arr);
// 45
1.
if
(
typeof
(console) ===
'undefined'
) {
2.
window.console = {
3.
log:
function
(msg) {
4.
alert(msg);
5.
}
6.
};
7.
}
8.
console.log(
'debug info.'
);
1.
var
undefined =
'Hello'
;
2.
undefined;
// 'Hello'
1.
var
name;
2.
name === undefined;
// true
1.
name2 === undefined;
// error – name2 is not defined
1.
typeof
(name2) === ‘undefined’;
// true
1.
var
img =
new
Image();
2.
img.src =
"clock2.gif"
;
1.
<
img
src
=
"clock.gif"
mce_src
=
"clock.gif"
alt
=
""
2.
onmouseover
=
"this.src='clock2.gif';"
3.
onmouseout
=
"this.src=clock.gif';"
/>
1.
var
source = [
'img1.gif'
,
'img2.gif'
];
2.
var
img =
new
Image();
3.
for
(
var
i = 0; i < source.length; i++) {
4.
img.src = source[i];
5.
}
1.
var
source = [
'img1.gif'
,
'img2.gif'
];
2.
for
(
var
i = 0; i < source.length; i++) {
3.
var
img =
new
Image();
4.
img.src = source[i];
5.
}
1.
function
add(i) {
2.
return
function
() {
3.
return
++i;
4.
};
5.
}
6.
add(2).toString();
// "function () { return ++i; }"
7.
add(2)();
// 3
1.
var
person = {
2.
_name:
''
,
3.
getName:
function
() {
4.
return
this
._name ||
'not defined'
;
5.
}
6.
};
7.
person.getName();
// "not defined"
1.
person._name;
// ""
01.
var
person = {};
02.
(
function
() {
03.
var
_name =
''
;
04.
person.getName =
function
() {
05.
return
_name ||
'not defined'
;
06.
}
07.
})();
08.
09.
person.getName();
// "not defined"
10.
typeof
(person._name);
// "undefined"
1.
for
(
var
i = 0; i < 2; i ++) {
2.
3.
}
4.
i;
// 2
1.
(
function
(){
2.
for
(
var
i = 0; i < 2; i ++) {
3.
4.
}
5.
})();
6.
typeof
(i) ===
'undefined'
;
// true
1.
NaN === NaN;
// false
1.
parseInt(
'hello'
, 10);
// NaN
2.
parseInt(
'hello'
, 10) == NaN;
// false
3.
parseInt(
'hello'
, 10) === NaN;
// false
1.
isNaN(parseInt(
'hello'
, 10));
// true
1.
if
(obj === undefined || obj ===
null
) {
2.
}
1.
if
(!obj) {
2.
3.
}
1.
function
add() {
2.
arguments.push(
'new value'
);
3.
}
4.
add();
// error - arguments.push is not a function
1.
function
add() {
2.
Array.prototype.push.call(arguments,
'new value'
);
3.
return
arguments;
4.
}
5.
add()[0];
// "new value"
1.
Boolean(
false
) ===
false
;
// true
2.
Boolean(
''
) ===
false
;
// true
1.
new
Boolean(
false
) ===
false
;
// false
2.
new
Boolean(
false
) ==
false
;
// true
3.
typeof
(
new
Boolean(
false
));
// "object"
4.
typeof
(Boolean(
false
));
// "boolean"
1.
var
startTime =
new
Date();
2.
var
str =
''
;
3.
for
(
var
i = 0; i < 50000; i++) {
4.
str += i;
5.
}
6.
alert(
new
Date() - startTime);
// Firefox - 18ms, IE7 - 2060ms
1.
var
startTime =
new
Date();
2.
var
arr = [];
3.
for
(
var
i = 0; i < 100000; i++) {
4.
arr.push(i);
5.
}
6.
var
str = arr.join(
""
);
7.
alert(
new
Date() - startTime);
// Firefox - 38ms, IE7 - 280ms
在JavaScript中,我们可以在字符串之前使用一元操作符“+”。这将会把字符串转化为数字,如果转化失败则返回NaN。
1.
2 +
'1'
;
// "21"
2.
2 + ( +
'1'
);
// 3
如果把 + 用在非字符串的前面,将按照如下顺序进行尝试转化:
- 调用valueOf()
- 调用toString()
- 转化为数字
1.
+
new
Date;
// 1242616452016
2.
+
new
Date ===
new
Date().getTime();
// true
3.
+
new
Date() === Number(
new
Date)
// true
1.
'index.jsp?page='
+encodeURI(
'/page/home.jsp'
);
// "index.jsp?page=/page/home.jsp"
2.
'index.jsp?page='
+encodeURIComponent(
'/page/home.jsp'
);
// "index.jsp?page=%2Fpage%2Fhome.jsp"
1.
<
div
id
=
"container1"
> </
div
>
1.
document.getElementById(
'container1'
).innerHTML =
"Hello World!"
;
1.
<
table
id
=
"table1"
> </
table
>
1.
// works well in Firefox, but fail to work in IE
2.
document.getElementById(
'table1'
).innerHTML =
"<tr><td>Hello</td><td>World!</td></tr>"
;
1.
<
div
id
=
"table1"
> </
div
>
1.
document.getElementById(
'table1'
).innerHTML =
"<table><tr><td>Hello</td><td>World!</td></tr></table>"
;
1.
0.1 + 0.2;
// 0.30000000000000004
1.
(0.1 + 0.2).toFixed();
// "0"
2.
(0.1 + 0.2).toFixed(1);
// "0.3"