var f = function g() {
return 23;
};
typeof g(); // error g is not defined複製代碼
在javaScript裏,聲明函數只有2種方法java
第一種:函數聲明windows
function foo(){...}
複製代碼
第二種:函數表達式bash
var foo = function(){...}
複製代碼
其餘如var f = function bar() {....}等聲明都按第二種方法處理,函數外部沒法經過bar訪問到函數,由於是一個表達式閉包
如下代碼存在幾個變量沒有被回收? 函數
var i = 1;
var i = 2;
var add = function() {
var i = 0;
return function(){
i++;
console.log(i);
}}();
add();複製代碼
變量回收規則有三點ui
1.全局變量不會被回收this
2.局部變量會被回收,函數執行完後內部的東西都會被銷燬spa
3.若是變量被另一個做用域引用就不會被回收prototype
由此能夠得知有三個變量沒有被回收,分別是全局變量i、add 還有閉包中的icode
var x = new Boolean(false);
if (x) {
alert('hi');
}
var y = Boolean(0);
if (y) {
alert('hello');
}複製代碼
輸出: hi複製代碼
new Boolean 與 Boolean的區別在於:用new調用構造函數會新建一個布爾對象,因此此處的x爲對象,任何對象轉布爾值都爲true!
而Boolean,此處沒有new, 進行的是顯式轉換,0轉布爾值爲false
順序:事件捕獲 -> 事件處理 -> 事件冒泡
描述:先事件捕獲從windows > document 往下級直到特定事件節點,而後進行事件處理,再事件冒泡,從節點往上級冒泡
var A = {n:4399}
var B = function () {
this.n = 9999
}
var C = function () {
this.n = 9999
}
B.prototype = A
C.prototype = A
var b = new B()
var c = new C()
A.n++;
console.log(b.n) // 9999
console.log(c.n) // 4400複製代碼
此題考查的是:在查找 b.n 是首先查找 b 對象自身有沒有 n 屬性,若是沒有會去原型(prototype)上查找