4.1 函數(shù)對象
- 對象字面量產(chǎn)生的對象連接到object.prototype,函數(shù)對象連接到Function.prototype(該原型對象本身也連接到object.prototype)
4.3 調(diào)用 Invocation
- 函數(shù)被調(diào)用時(shí)咖摹,接收兩個(gè)附加的參數(shù):this和arguments
- 當(dāng)實(shí)際參數(shù)(arguments)與形式參數(shù)(parameters)的個(gè)數(shù)不匹配時(shí)不會(huì)導(dǎo)致運(yùn)行時(shí)錯(cuò)誤蝙泼。
- 實(shí)際參數(shù)過多疙渣,超出的參數(shù)值被忽略
- 過少做粤,缺失的值被替換為undefined。
- 對參數(shù)值不會(huì)進(jìn)行類型檢查:任何類型的值都可以傳遞
javascript的四種調(diào)用模式
-
方法調(diào)用模式 The Method Invocation Pattern
當(dāng)一個(gè)方法被調(diào)用時(shí)司抱,this被綁定到該對象
永罚。
var myObject = {
value:0,
increment: function(inc){
this.value += typeof inc === 'number'?inc:1;
}
};
myObject.increment();
document.writeln(myObject.value); //1
myObject.increment(2);
document.writeln(myObject.value); //3
this到對象的綁定發(fā)生在調(diào)用的時(shí)候。
通過this可取得它們所屬對象的上下文的方法稱為公共方法宋舷。
-
函數(shù)調(diào)用模式 The Function Invocation Pattern
當(dāng)一個(gè)函數(shù)不是對象的屬性的時(shí)候,它被當(dāng)作函數(shù)來調(diào)用瓢姻。此時(shí)this綁定到全局對象
祝蝠。
這是個(gè)錯(cuò)誤的設(shè)計(jì),因?yàn)榉椒ㄔ谡{(diào)用內(nèi)部函數(shù)時(shí)幻碱,this本應(yīng)該綁定到外部函數(shù)的this绎狭,這使得方法不能利用內(nèi)部函數(shù)來幫助它工作,這里有個(gè)解決辦法:
myObject.double = function(){
var that = this ;//解決方法
var helper = function(){
that.value = add(that.value,that.value);
}
helper();//函數(shù)調(diào)用模式
}
myObject.double();
document.writeln(myObject.value);//6
-
構(gòu)造器調(diào)用模式 The Constructor Invocation Pattern
Js是一門基于原型繼承
的語言褥傍,但也提供了一套和基于類的語言相似的對象構(gòu)建方法儡嘶。
如果在一個(gè)函數(shù)前面帶上new來調(diào)用,那么將創(chuàng)建一個(gè)隱藏連接到該函數(shù)的prototype成員的新對象恍风,同時(shí)this將會(huì)綁定到那個(gè)新對象上蹦狂。
var Quo = function (string){
this.status = string;
};
Quo.prototype.get_status = function (){
return this.status;
};
var myQuo = new Quo("confused");
document.writeln(myQuo.get_status());
約定構(gòu)造器函數(shù)要大寫!
順便不推薦這種方式朋贬,下一章有更好的替代方法凯楔。
-
Apply調(diào)用模式 The Apply Invocation Pattern
apply方法接收兩個(gè)參數(shù),第一個(gè)綁定到this锦募,第二個(gè)是參數(shù)數(shù)組摆屯。
var array = [3,4];
var sum = add.apply(null,array);
document.writeln(sum);
var statusObject = {
status : 'A-OK'
};
//雖然statusObject不繼承于 Quo.prototype,但依然可以調(diào)用get_status方法
var status = Quo.prototype.get_status.apply(statusObject);
document.write(status);
4.5 返回 Return
- return語句可用來使函數(shù)提前返回糠亩。
- 一個(gè)函數(shù)總是會(huì)返回一個(gè)值虐骑,如果沒有指定返回值,則返回undefined赎线。
- 如果函數(shù)以在前面加上new前綴的方式來調(diào)用廷没,且返回值不是一個(gè)對象,則返回this(該新對象)
4.6 異常 Exceptions
JS提供了一套異常處理機(jī)制垂寥,throw語句中斷函數(shù)的執(zhí)行腕柜。它應(yīng)該拋出一個(gè)exception對象,該對象包含可識(shí)別異常類型的name屬性和一個(gè)描述性的message屬性矫废。也可以添加其他屬性。
該exception對象將被傳遞到一個(gè)try語句的catch從句
var add2 = function(a,b){
if(typeof a !== 'number' || typeof b !== 'number'){
throw {
name: 'TypeError',
message: 'add needs numbers'
};
}
return a+b;
}
var try_it = function(){
try {
add2('seven');
}catch(e){
document.writeln(e.name+':'+e.message);
}
}
try_it();
4.7 給類型增加方法 Augmenting Types
為什么這里是給Function增加方法而不是Object砰蠢?Number是在Function的原型鏈上的嗎蓖扑?
JS允許給語言的基本類型增加方法。
//定義新方法,這樣就不用總是輸入prototype了
Function.prototype.method = function(name, func) {
if(!this.prototype[name]) {
this.prototype[name] = func;
return this;
}
}
Number.method('integer', function() {
return Math[this < 0 ? 'ceil' : 'floor'](this);
})