FUNCTION
V8 中還提供了 .bind 用來改變this的引用:
function a() {
return this.hello == 'hello';
}
var b = a.bind({ hello: 'hello' });
b(); //=>true
V8 中還支持到非標準的函數(shù)屬性命名,并可以使用 .name 來獲取命名:
var a = function b() {}
a.name; //=>b
該屬性用于 V8 內部的堆棧追蹤。當有錯誤拋出時记盒,V8 會顯示一個堆棧信息归露,并會告訴你是那個函數(shù)出錯了币砂。因此救崔,為函數(shù)命名有助于調試,所以推薦暂论。
__PROTO__(繼承)
__proto__ 使得定義繼承鏈更容易:
function Animal() {}
Animal.prototype.dsd = function(e) {
return e;
}
function Dog() {}
// 使用__proto__實現(xiàn)繼承
Dog.prototype.__proto__ = Animal.prototype;
var dog = new Dog();
dog.dsd(110); //=>110
存取器
可以通過 __defineSetter__ 來設置屬性, __defineGetter__ 訪問屬性空免。
下面這個例子空另,為所有的 String 對象添加了 student 獲取器,它會執(zhí)行設定好的邏輯蹋砚,簡單的訪問該屬性就會調用事先設定好的函數(shù)扼菠,無需顯性調用:
String.prototype.__defineGetter__('student', function() {
return '今天是' + this + '值日';
})
var a = new String('小明');
a.student; //=>今天是小明值日