你认为每个程序员都应该知道JavaScript的哪些“隐藏特性”?
在看到以下问题的优质答案后,我认为是时候向JavaScript请求它了。
HTML的隐藏特性 CSS的隐藏特性 PHP的隐藏特性 ASP的隐藏特性。网 c#的隐藏特性 Java的隐藏特性 Python的隐藏特性
尽管JavaScript可以说是目前最重要的客户端语言(问问谷歌就知道了),但令人惊讶的是,大多数web开发人员很少意识到它的强大。
你认为每个程序员都应该知道JavaScript的哪些“隐藏特性”?
在看到以下问题的优质答案后,我认为是时候向JavaScript请求它了。
HTML的隐藏特性 CSS的隐藏特性 PHP的隐藏特性 ASP的隐藏特性。网 c#的隐藏特性 Java的隐藏特性 Python的隐藏特性
尽管JavaScript可以说是目前最重要的客户端语言(问问谷歌就知道了),但令人惊讶的是,大多数web开发人员很少意识到它的强大。
当前回答
模块模式
<script type="text/javascript">
(function() {
function init() {
// ...
}
window.onload = init;
})();
</script>
没有使用var语句或在函数外部声明的变量和函数将在全局作用域中定义。如果一个同名的变量/函数已经存在,它将被无声地覆盖,这可能导致很难发现错误。一种常见的解决方案是将整个代码体封装到一个匿名函数中并立即执行它。这样,所有变量/函数都定义在匿名函数的作用域中,不会泄漏到全局作用域中。
要显式地在全局作用域定义一个变量/函数,它们必须以window为前缀:
window.GLOBAL_VAR = 12;
window.global_function = function() {};
其他回答
在函数中,你可以返回函数本身:
function showSomething(a){
alert(a);
return arguments.callee;
}
// Alerts: 'a', 'b', 'c'
showSomething('a')('b')('c');
// Or what about this:
(function (a){
alert(a);
return arguments.callee;
})('a')('b')('c');
我不知道什么时候它会有用,不管怎样,它很奇怪也很有趣:
var count = function(counter){
alert(counter);
if(counter < 10){
return arguments.callee(counter+1);
}
return arguments.callee;
};
count(5)(9); // Will alert 5, 6, 7, 8, 9, 10 and 9, 10
实际上,Node.js的FAB框架似乎已经实现了这个功能;例如,请参阅本主题。
令人惊讶的是,很多人都没有意识到它也是面向对象的。
下面是一些有趣的事情:
Comparing NaN with anything (even NaN) is always false, that includes ==, < and >. NaN Stands for Not a Number but if you ask for the type it actually returns a number. Array.sort can take a comparator function and is called by a quicksort-like driver (depends on implementation). Regular expression "constants" can maintain state, like the last thing they matched. Some versions of JavaScript allow you to access $0, $1, $2 members on a regex. null is unlike anything else. It is neither an object, a boolean, a number, a string, nor undefined. It's a bit like an "alternate" undefined. (Note: typeof null == "object") In the outermost context, this yields the otherwise unnameable [Global] object. Declaring a variable with var, instead of just relying on automatic declaration of the variable gives the runtime a real chance of optimizing access to that variable The with construct will destroy such optimzations Variable names can contain Unicode characters. JavaScript regular expressions are not actually regular. They are based on Perl's regexs, and it is possible to construct expressions with lookaheads that take a very, very long time to evaluate. Blocks can be labeled and used as the targets of break. Loops can be labeled and used as the target of continue. Arrays are not sparse. Setting the 1000th element of an otherwise empty array should fill it with undefined. (depends on implementation) if (new Boolean(false)) {...} will execute the {...} block Javascript's regular expression engine's are implementation specific: e.g. it is possible to write "non-portable" regular expressions.
[更新了一点,以回应良好的评论;请参阅评论]
如果你盲目地eval()一个JSON字符串来反序列化它,你可能会遇到问题:
这并不安全。该字符串可能包含恶意函数调用! 如果你没有将JSON字符串括在括号中,属性名可能会被误认为是标签,导致意外的行为或语法错误: Eval ("{\"foo\": 42}");//语法错误:无效标签 Eval ("({\"foo\": 42})");/ /好吧
JavaScript没有块作用域(但它有闭包,所以让我们称它为偶数?)
var x = 1;
{
var x = 2;
}
alert(x); // outputs 2