你认为每个程序员都应该知道JavaScript的哪些“隐藏特性”?
在看到以下问题的优质答案后,我认为是时候向JavaScript请求它了。
HTML的隐藏特性 CSS的隐藏特性 PHP的隐藏特性 ASP的隐藏特性。网 c#的隐藏特性 Java的隐藏特性 Python的隐藏特性
尽管JavaScript可以说是目前最重要的客户端语言(问问谷歌就知道了),但令人惊讶的是,大多数web开发人员很少意识到它的强大。
你认为每个程序员都应该知道JavaScript的哪些“隐藏特性”?
在看到以下问题的优质答案后,我认为是时候向JavaScript请求它了。
HTML的隐藏特性 CSS的隐藏特性 PHP的隐藏特性 ASP的隐藏特性。网 c#的隐藏特性 Java的隐藏特性 Python的隐藏特性
尽管JavaScript可以说是目前最重要的客户端语言(问问谷歌就知道了),但令人惊讶的是,大多数web开发人员很少意识到它的强大。
当前回答
这似乎只适用于Firefox (SpiderMonkey)。函数内部:
Arguments[-2]给出了参数的个数(与Arguments .length相同) Arguments[-3]给出了被调用的函数(与Arguments .callee相同)
其他回答
window.name的值在页面更改时保持不变,如果在同一个域中(如果在iframe中,使用document),可以被父窗口读取。getElementById(“你的帧的ID”). contentwindow .name来访问它),并且只受可用内存的限制。
生成器和迭代器(仅适用于Firefox 2+和Safari)。
function fib() {
var i = 0, j = 1;
while (true) {
yield i;
var t = i;
i = j;
j += t;
}
}
var g = fib();
for (var i = 0; i < 10; i++) {
document.write(g.next() + "<br>\n");
}
The function containing the yield keyword is a generator. When you call it, its formal parameters are bound to actual arguments, but its body isn't actually evaluated. Instead, a generator-iterator is returned. Each call to the generator-iterator's next() method performs another pass through the iterative algorithm. Each step's value is the value specified by the yield keyword. Think of yield as the generator-iterator version of return, indicating the boundary between each iteration of the algorithm. Each time you call next(), the generator code resumes from the statement following the yield. In normal usage, iterator objects are "invisible"; you won't need to operate on them explicitly, but will instead use JavaScript's for...in and for each...in statements to loop naturally over the keys and/or values of objects.
var objectWithIterator = getObjectSomehow();
for (var i in objectWithIterator)
{
document.write(objectWithIterator[i] + "<br>\n");
}
我要说的是自执行函数。
(function() { alert("hi there");})();
因为Javascript没有块作用域,如果你想定义局部变量,你可以使用一个自执行函数:
(function() {
var myvar = 2;
alert(myvar);
})();
在这里,myvar不会干扰或污染全局作用域,并且在函数终止时消失。
所有“隐藏的”特性都在Mozilla wiki上:http://developer.mozilla.org/en/JavaScript。
这里有JavaScript 1.5的核心参考,JavaScript 1.6的新特性,JavaScript 1.7的新特性,以及JavaScript 1.8的新特性。看看所有这些实际工作的例子,是正确的。
在这个帖子中有几个答案告诉你如何去做 通过Array对象的原型扩展Array对象。这很糟糕 IDEA,因为它破坏了语句中的for (i)。
如果你不使用for (i in a) 代码中的任何地方?除非你自己的代码是 只有您正在运行的代码,这是不太可能的 在浏览器内部。我担心如果人们开始扩张 他们的数组对象像这样,堆栈溢出将启动 充斥着一堆神秘的JavaScript错误。
点击这里查看有用的细节。