深入了解Function教程
21视频教程网3月28日整理
//通过A调用只有一个参数的aFunc():
chooseFuncBy("A")("A func has only one parameter.");
//输出:aFunc():A func has only one parameter.
//通过B调用有两个参数的bFunc():
chooseFuncBy("B")("B func has two parameters.","No.2 parameter");
//输出:bFunc():B func has two parameters. one more Parameter:No.2 parameter
//字符串不符,默认函数
chooseFuncBy("wu lala")("I choose A function");
//输出:Welcome to Kingda.org! My blog
var withObj:Object = new Object();
var myObj:Object = {name:"黑羽", blog:"http;//www.kingda.org", hobby:"Starcraft"};
chooseFuncBy(withObj)(myObj);
/* 输出:
objectFunc():
name:黑羽
blog:http;//www.kingda.org
hobby:Starcraft
*/
function chooseFuncBy(inputString):Function {
//运用一:利用参数的种类来确定返回的函数
if (!(typeof(inputString) == "string")) {
return objectFunc;
}
//运用二:根据参数内容来返回函数
switch (inputString) {
case "A":
return aFunc;
case "B":
return bFunc;
default:
return kingdaFunc;
}
//更多延伸运用:利用参数个数、instanceof确定不同Class的实例来选择函数等等
}
function aFunc(nS:String):Void {
trace ("aFunc():" + nS);
}
function bFunc(nS:String, nP:String):Void {
trace ("bFunc():" + nS + " one more Parameter:"+ nP);
}
function kingdaFunc():Void {
trace ("Welcome to Kingda.org! My blog");
}
function objectFunc(kingdaObj:Object):Void {
trace ("objectFunc():");
for (var i in kingdaObj) {
trace ( i + ":" + kingdaObj[i]);
}
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |