varninja={};addMethod(ninja,'whatever',function(a){console.log(arguments.length);});addMethod(ninja,'whatever',function(a,b){console.log(arguments.length);});addMethod(ninja,'whatever',function(){console.log(arguments.length);});functionaddMethod(object,name,fn){varold=object[name];object[name]=function(){if(fn.length==arguments.length)returnfn.apply(this,arguments);elseif(typeofold=='function')returnold.apply(this,arguments);};}ninja.whatever();這是一段出自《SecretsoftheJavaScriptNinja》的代碼,作者說(shuō)它的作用是實(shí)現(xiàn)函數(shù)的重載,但是對(duì)于整個(gè)的運(yùn)行過(guò)程我還是理解不透,麻煩懂的人解釋一下。
解釋一下這段代碼的含義
眼眸繁星
2019-04-21 20:38:33