JavaScript tips and tricks - 2
2009-05-13 14:41
686 查看
Always specify the second argument - parseInt
parseInt converts a string to an int number, the syntax is:
The second argument is optional, which specify the radix of the first argument.
If you omit radix, following the rules:
->if the string begins with ‘0x’, the radix is 16.
->if the string begins with ‘0’, the radix is 8.
->otherwise, the radix is 10.
Therefore, the following code will confuse somebody who don’t know this rules:
Delete an element from an array
Whether can we use delete keyword to achieve this:
You can see, delete can’t really delete an item. The removed item is replace with an undefined value, the array’s length is not reduced.In fact, the splice method existing in the Array.prototype can be helpful:
Function as object
Function in javascript is also object; therefore we can assign properties even functions to function.
See example below:
We assign a count property to function to record how many times the function is called.
This can be done in a more elegant way:
arguments.callee refer to the function which is current running.
Find the max value in an array
There is an array contains all of number, how to find out the max value.
This also works, but we all know there is a Math object in Javascript:
Can this be helpful? yes
Add console.log support in IE
We often use console.log to debug javascript in firefox with firebug support.
But it will break down IE’ execution, because IE doesn’t has console object, we can simple fix it like this:
parseInt converts a string to an int number, the syntax is:
parseInt(str, [radix])
The second argument is optional, which specify the radix of the first argument.
If you omit radix, following the rules:
->if the string begins with ‘0x’, the radix is 16.
->if the string begins with ‘0’, the radix is 8.
->otherwise, the radix is 10.
Therefore, the following code will confuse somebody who don’t know this rules:
parseInt('08'); // 0 parseInt('08', 10); // 8
Delete an element from an array
Whether can we use delete keyword to achieve this:
var arr = [1, 2, 3, 4, 5]; delete arr[1]; arr; // [1, undefined, 3, 4, 5]
You can see, delete can’t really delete an item. The removed item is replace with an undefined value, the array’s length is not reduced.In fact, the splice method existing in the Array.prototype can be helpful:
var arr = [1, 2, 3, 4, 5]; arr.splice(1, 1); arr; // [1, 3, 4, 5]
Function as object
Function in javascript is also object; therefore we can assign properties even functions to function.
See example below:
function add() { return add.count++; } add.count = 0; add(); // 0 add(); // 1 add(); // 2
We assign a count property to function to record how many times the function is called.
This can be done in a more elegant way:
function add() { if(!arguments.callee.count) { arguments.callee.count = 0; } return arguments.callee.count++; } add(); // 0 add(); // 1 add(); // 2
arguments.callee refer to the function which is current running.
Find the max value in an array
There is an array contains all of number, how to find out the max value.
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
This also works, but we all know there is a Math object in Javascript:
Math.max(2, 3, 45, 12, 8); // 45
Can this be helpful? yes
var arr = [2, 3, 45, 12, 8]; Math.max.apply(null, arr); // 45
Add console.log support in IE
We often use console.log to debug javascript in firefox with firebug support.
But it will break down IE’ execution, because IE doesn’t has console object, we can simple fix it like this:
if (typeof(console) === 'undefined') { window.console = { log: function(msg) { alert(msg); } }; } console.log('debug info.');
相关文章推荐
- JavaScript tips and tricks - 1
- 转:21 JavaScript Tips and Tricks for JavaScript Developers
- 45 Useful JavaScript Tips, Tricks and Best Practices
- 45 Useful JavaScript Tips, Tricks and Best Practices
- JavaScript tips and tricks - 2
- 45 Useful JavaScript Tips, Tricks and Best Practices(有用的JavaScript技巧,技巧和最佳实践)
- JavaScript tips and tricks - 1
- 45 Useful JavaScript Tips, Tricks and Best Practices
- Useful JavaScript Tips, Tricks and Best Practices
- JavaScript tips and tricks - 5
- 21 JavaScript Tips And Tricks For JavaScript Developers
- 转:45 Useful JavaScript Tips, Tricks and Best Practices
- 45 Useful JavaScript Tips, Tricks and Best Practices
- WWDC笔记:2011 Session 125 UITableView Changes, Tips and Tricks
- Android Studio 100 tips and tricks
- Rspec tips and tricks
- PHP: APC Configuration and Usage Tips and Tricks
- Microsoft Visual C++ Tips and Tricks
- How to Code .NET: Tips and Tricks for Coding .NET 1.1 and .NET 2.0 Applications Effectively
- debugging Auto Layout:Debugging Tricks and Tips