0 == false; // true
1 == true; // true
'' == false // true
null == false // true
0 === false; // false
1 === true; // false
'' === false // false
null === false // false
!!0 === false; // true
!!1 === true; // true
!!'' === false // true
!!null === false // true
function plus(base, added) {
return base + added;
}
plus(2); // NaN
function plus(base, added) {
added = added || 1;
return base + added;
}
plus(2); // 3
plus(2, 2); // 4
有网友提到 plus(2, 0) = 3; 的确是这样的,看来这个地方还要做一些特殊处理:
function plus(base, added) {
added = added || (added === 0 ? 0 : 1);
return base + added;
}
if(top !== window) {
top.location.href = window.location.href;
}
'Hello world, hello world'.replace('world', 'JavaScript');
// The result is "Hello JavaScript, hello world"
'Hello world, hello world'.replace(/world/g, 'JavaScript');
// The result is "Hello JavaScript, hello JavaScript"
'Hello world, hello world'.replace(/hello/gi, 'Hi');
// The result is "Hi world, Hi world"
function args() {
return [].slice.call(arguments, 0);
}
args(2, 5, 8); // [2, 5, 8]
parseInt(str, [radix])
parseInt('08'); // 0
parseInt('08', 10); // 8
var arr = [1, 2, 3, 4, 5];
delete arr[1];
arr; // [1, undefined, 3, 4, 5]
事实上,我们可以通过Array.prototype中的splice 函数来删除数组中的元素,如下所示:
var arr = [1, 2, 3, 4, 5];
arr.splice(1, 1);
arr; // [1, 3, 4, 5]
function add() {
return add.count++;
}
add.count = 0;
add(); // 0
add(); // 1
add(); // 2
当然这可以通过更优雅的方式来实现:
function add() {
if(!arguments.callee.count) {
arguments.callee.count = 0;
}
return arguments.callee.count++;
}
add(); // 0
add(); // 1
add(); // 2
var arr = [2, 3, 45, 12, 8];
var max = arr[0];
for(var i in arr) {
if(arr[i] > max) {
max = arr[i];
}
}
max; // 45
Math.max(2, 3, 45, 12, 8); // 45
var arr = [2, 3, 45, 12, 8];
Math.max.apply(null, arr); // 45
if (typeof(console) === 'undefined') {
window.console = {
log: function(msg) {
alert(msg);
}
};
}
console.log('debug info.');
var undefined = 'Hello';
undefined; // 'Hello'
var name;
name === undefined; // true
name2 === undefined; // error – name2 is not defined
typeof(name2) === ‘undefined'; // true
var img = new Image();
img.src = "clock2.gif";
onmouseover="this.src='clock2.gif';"
onmouseout="this.src=https://www.gxlcms.com/clock.gif';" />
var source = ['img1.gif','img2.gif'];
var img = new Image();
for(var i = 0; i < source.length; i++) {
img.src = source[i];
}
var source = ['img1.gif','img2.gif'];
for(var i = 0; i < source.length; i++) {
var img = new Image();
img.src = source[i];
}
function add(i) {
return function() {
return ++i;
};
}
add(2).toString(); // "function () { return ++i; }"
add(2)(); // 3
var person = {
_name: '',
getName: function() {
return this._name || 'not defined';
}
};
person.getName(); // "not defined"
person._name; // ""
var person = {};
(function() {
var _name = '';
person.getName = function() {
return _name || 'not defined';
}
})();person.getName(); // "not defined"
typeof(person._name); // "undefined"
for(var i = 0; i < 2; i ++) {}
i; // 2
(function (){
for(var i = 0; i < 2; i ++) {}
})();
typeof(i) === 'undefined'; // true
NaN === NaN; // false
parseInt('hello', 10); // NaN
parseInt('hello', 10) == NaN; // false
parseInt('hello', 10) === NaN; // false
isNaN(parseInt('hello', 10)); // true
if(obj === undefined || obj === null) {
}
if(!obj) {}
function add() {
arguments.push('new value');
}
add(); // error - arguments.push is not a function
function add() {
Array.prototype.push.call(arguments, 'new value');
return arguments;
}
add()[0]; // "new value"
Boolean(false) === false; // true
Boolean('') === false; // true
new Boolean(false) === false; // false
new Boolean(false) == false; // true
typeof(new Boolean(false)); // "object"
typeof(Boolean(false)); // "boolean"
var startTime = new Date();
var str = '';
for (var i = 0; i < 50000; i++) {
str += i;
}
alert(new Date() - startTime); // Firefox - 18ms, IE7 - 2060ms
var startTime = new Date();
var arr = [];
for (var i = 0; i < 100000; i++) {
arr.push(i);
}
var str = arr.join("");
alert(new Date() - startTime); // Firefox - 38ms, IE7 - 280ms
2 + '1'; // "21"
2 + ( +'1'); // 3
+new Date; // 1242616452016
+new Date === new Date().getTime(); // true
+new Date() === Number(new Date) // true
'index.jsp?page='+encodeURI('/page/home.jsp'); // "index.jsp?page=/page/home.jsp"
'index.jsp?page='+encodeURIComponent('/page/home.jsp'); // "index.jsp?page=%2Fpage%2Fhome.jsp"
document.getElementById('container1').innerHTML = "Hello World!";
// works well in Firefox, but fail to work in IE
document.getElementById('table1').innerHTML = ""; Hello World!
那么如果动态的创建一个table呢,下面提供了一种可行的方法:
document.getElementById('table1').innerHTML = "
Hello | World! |
0.1 + 0.2; // 0.30000000000000004
(0.1 + 0.2).toFixed(); // "0"
(0.1 + 0.2).toFixed(1); // "0.3"
定义数组:
var strweek= new Array(7);
问号表达式
var i= (condition)?A:B;
相当于if-else 语句;condition 成立 执行A ,不成立执行B;
switch 语句
var i=3;
var result="";
swithck(i);
{
case 1;
result="First";
case 2;
result="Second";
case 3;
result="Three";
break;
}
Date类
getDate() getYear() getMont()
getMinutes() getHours() getSeconds()
setTimeout("fution()",1000);
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com