0 == false; // true<BR> 1 == true; // true<BR> '' == false // true<BR> null == false // true<BR>
0 === false; // false<BR> 1 === true; // false<BR> '' === false // false<BR> null === false // false<BR>
!!0 === false; // true<BR> !!1 === true; // true<BR> !!'' === false // true<BR> !!null === false // true<BR>
function plus(base, added) {<BR>
return base + added;<BR>
}<BR>
plus(2); // NaN<BR>
function plus(base, added) {<BR>
added = added || 1;<BR>
return base + added;<BR>
}<BR>
plus(2); // 3<BR>
plus(2, 2); // 4<BR>
function plus(base, added) {<BR>
added = added || (added === 0 ? 0 : 1);<BR>
return base + added;<BR>
}<BR>
if(top !== window) {<BR>
top.location.href = window.location.href;<BR>
}<BR>
'Hello world, hello world'.replace('world', 'JavaScript');<BR>
// The result is "Hello JavaScript, hello world"<BR>
'Hello world, hello world'.replace(/world/g, 'JavaScript');<BR> // The result is "Hello JavaScript, hello JavaScript"<BR>
'Hello world, hello world'.replace(/hello/gi, 'Hi');<BR> // The result is "Hi world, Hi world"<BR>
function args() {<BR>
return [].slice.call(arguments, 0);<BR>
}<BR>
args(2, 5, 8); // [2, 5, 8]<BR>
parseInt(str, [radix])<BR>
parseInt('08'); // 0<BR>
parseInt('08', 10); // 8<BR>
var arr = [1, 2, 3, 4, 5];<BR> delete arr[1];<BR> arr; // [1, undefined, 3, 4, 5]<BR>
var arr = [1, 2, 3, 4, 5];<BR> arr.splice(1, 1);<BR> arr; // [1, 3, 4, 5]<BR>
function add() {<BR>
return add.count++;<BR>
}<BR>
add.count = 0;<BR>
add(); // 0<BR>
add(); // 1<BR>
add(); // 2<BR>
function add() {<BR>
if(!arguments.callee.count) {<BR>
arguments.callee.count = 0;<BR>
}<BR>
return arguments.callee.count++;<BR>
}<BR>
add(); // 0<BR>
add(); // 1<BR>
add(); // 2<BR>
var arr = [2, 3, 45, 12, 8];<BR>
var max = arr[0];<BR>
for(var i in arr) {<BR>
if(arr[i] > max) {<BR>
max = arr[i];<BR>
}<BR>
}<BR>
max; // 45<BR>
Math.max(2, 3, 45, 12, 8); // 45<BR>
var arr = [2, 3, 45, 12, 8];<BR> Math.max.apply(null, arr); // 45<BR>
if (typeof(console) === 'undefined') {<BR>
window.console = {<BR>
log: function(msg) {<BR>
alert(msg);<BR>
}<BR>
};<BR>
}<BR>
console.log('debug info.');<BR>
var undefined = 'Hello'; <BR> undefined; // 'Hello'<BR>
var name; <BR> name === undefined; // true<BR>
name2 === undefined; // error – name2 is not defined<BR>
typeof(name2) === ‘undefined'; // true<BR>
var img = new Image(); <BR> img.src = "clock2.gif";<BR>
@@##@@
onmouseover="this.src='clock2.gif';" <BR>
onmouseout="this.src=clock.gif';" /><BR>
var source = ['img1.gif','img2.gif']; <BR>
var img = new Image(); <BR>
for(var i = 0; i < source.length; i++) { <BR>
img.src = source[i]; <BR>
}<BR>
var source = ['img1.gif','img2.gif']; <BR>
for(var i = 0; i < source.length; i++) { <BR>
var img = new Image(); <BR>
img.src = source[i]; <BR>
}<BR>
function add(i) { <BR>
return function() { <BR>
return ++i; <BR>
}; <BR>
} <BR>
add(2).toString(); // "function () { return ++i; }" <BR>
add(2)(); // 3<BR>
var person = { <BR>
_name: '', <BR>
getName: function() { <BR>
return this._name || 'not defined'; <BR>
} <BR>
}; <BR>
person.getName(); // "not defined"<BR>
person._name; // ""<BR>
var person = {}; <BR>
(function() { <BR>
var _name = ''; <BR>
person.getName = function() { <BR>
return _name || 'not defined'; <BR>
} <BR>
})(); <br><br>
person.getName(); // "not defined" <BR>
typeof(person._name); // "undefined"<BR>
for(var i = 0; i < 2; i ++) { <br><br>
} <BR>
i; // 2<BR>
(function (){ <BR>
for(var i = 0; i < 2; i ++) { <br><br>
}<BR>
})(); <BR>
typeof(i) === 'undefined'; // true<BR>
NaN === NaN; // false<BR>
parseInt('hello', 10); // NaN <BR>
parseInt('hello', 10) == NaN; // false <BR>
parseInt('hello', 10) === NaN; // false<BR>
isNaN(parseInt('hello', 10)); // true<BR>
if(obj === undefined || obj === null) { <BR>
}<BR>
if(!obj) { <br><br>
}<BR>
function add() { <BR>
arguments.push('new value'); <BR>
} <BR>
add(); // error - arguments.push is not a function<BR>
function add() { <BR>
Array.prototype.push.call(arguments, 'new value'); <BR>
return arguments; <BR>
} <BR>
add()[0]; // "new value"<BR>
Boolean(false) === false; // true <BR>
Boolean('') === false; // true<BR>
new Boolean(false) === false; // false <BR> new Boolean(false) == false; // true <BR> typeof(new Boolean(false)); // "object" <BR> typeof(Boolean(false)); // "boolean"<BR>
var startTime = new Date();<BR>
var str = '';<BR>
for (var i = 0; i < 50000; i++) {<BR>
str += i;<BR>
}<BR>
alert(new Date() - startTime); // Firefox - 18ms, IE7 - 2060ms<BR>
var startTime = new Date();<BR>
var arr = [];<BR>
for (var i = 0; i < 100000; i++) {<BR>
arr.push(i);<BR>
}<BR>
var str = arr.join("");<BR>
alert(new Date() - startTime); // Firefox - 38ms, IE7 - 280ms<BR>
在JavaScript中,我们可以在字符串之前使用一元操作符“+”。这将会把字符串转化为数字,如果转化失败则返回NaN。
2 + '1'; // "21"<BR> 2 + ( +'1'); // 3<BR>
如果把 + 用在非字符串的前面,将按照如下顺序进行尝试转化:
- 调用valueOf()
- 调用toString()
- 转化为数字
+new Date; // 1241242616452016<BR> +new Date === new Date().getTime(); // true<BR> +new Date() === Number(new Date) // true<BR>
'index.jsp?page='+encodeURI('/page/home.jsp'); // "index.jsp?page=/page/home.jsp"<BR>
'index.jsp?page='+encodeURIComponent('/page/home.jsp'); // "index.jsp?page=%2Fpage%2Fhome.jsp"<BR>
<div id="container1"> </div><BR>
document.getElementById('container1').innerHTML = "Hello World!";<table id="table1"> </table><BR>
// works well in Firefox, but fail to work in IE<BR>
document.getElementById('table1').innerHTML = "<tr><td>Hello</td><td>World!</td></tr>";<BR>
<div id="table1"> </div><BR>
document.getElementById('table1').innerHTML = "<table><tr><td>Hello</td><td>World!</td></tr></table>";<BR>
0.1 + 0.2; // 0.30000000000000004
(0.1 + 0.2).toFixed(); // "0"<BR> (0.1 + 0.2).toFixed(1); // "0.3"
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号