Home >Web Front-end >JS Tutorial >JavaScript learning skills_javascript skills
0 == false; // true<br> 1 == true; // true<br> '' == false // true<br> null == false // true<br>But these values are not Boolean type.
0 === false; // false<br> 1 === true; // false<br> '' === false // false<br> null === false // false<br>The question now is how to convert other types into Boolean types:
!!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>plus(2) and plus(2, undefined) are equivalent, and the result of 2 undefined is NaN . The question now is, if the second parameter is not passed, how to assign an initial value to it?
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>head of each of your pages. If you want to know if anyone is using it in real life, just take a look at Baidu's blog and you will know.
'Hello world, hello world'.replace('world', 'JavaScript');<br> // The result is "Hello JavaScript, hello world"<br>replace The first parameter of the function is a regular expression. If you pass a string as the first parameter, only the first matching string found is replaced.
'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>If the second parameter is not passed, the following rules are followed:
parseInt('08'); // 0<br> parseInt('08', 10); // 8<br>So, be sure to specify the second parameter for
var arr = [1, 2, 3, 4, 5];<br> delete arr[1];<br> arr; // [1, undefined, 3, 4, 5]<br>delete doesn’t really work Deletes an element from the array. The deleted elements will be replaced by undefined, and the length of the array will not change.
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>count attribute to the function add to record the number of times this function has been called.
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>arguments.callee points to the currently running function.
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 object in JavaScript to process numbers: Then, we can find the maximum value in the array like this:
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>Is
var undefined = 'Hello'; <br> undefined; // 'Hello'<br>undefined It’s just a predefined variable in JavaScript. Note: Never do this in a JavaScript program, this trick just tells you that this is the case.
var name; <br> name === undefined; // true<br>2. 从来没有声明过此变量
name2 === undefined; // error – name2 is not defined<br>在第二种情况下,会有一个错误被抛出,那么如果判断一个变量是否为undefined而不产生错误呢?
typeof(name2) === ‘undefined'; // true<br>
var img = new Image(); <br> img.src = "clock2.gif";<br>
<img src="clock.gif" src="clock.gif" alt="" <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>add(2) 是一个函数,它可能获取外部函数的局部变量i 。
var person = { <BR> _name: '', <BR> getName: function() { <BR> return this._name || 'not defined'; <BR> } <BR> }; <BR> person.getName(); // "not defined"<BR>下划线前缀用来作为私有变量的约定,但是其他开发人员仍然可以调用此私有变量:
person._name; // ""<BR>那么,如何在JavaScript中创建一个真正的私有变量呢?
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>那么如何来检查一个值是否NaN?
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>这样会出错,因为arguments 不是一个真正的数组,没有push方法。
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>所以,Boolean(0) 和!!0 是等价的。
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; // 1242616452016<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>因此,在对URL进行编码时我们经常会选择 encodeURIComponent。
<div id="container1"> </div><br>
document.getElementById('container1').innerHTML = "Hello World!";但是在IE下设置table.innerHTML 将会导致错误:
<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>实际上,table, thead, tr, select等元素的innerHTML属性在IE下都是只读的。
<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你可以通过toFixed方法指定四舍五入的小数位数:
(0.1 + 0.2).toFixed(); // "0"<br> (0.1 + 0.2).toFixed(1); // "0.3"