标签:括号 cti white 第一个 结果 自己的 类对象 应用 func
//这是一个函数表达式//它做为一个參数传递给函数 CallMecallMe(function(){//这是命名函数表达式//也被称作匿名函数});//这是命名函数表达式callMe(function me(){//这里是命名函数表达式//而且其名称为me});//还有一个函数表达式var myobject = {say:function(){//这里是函数表达式}};
<script>function writeCode(callBack){console.log("callBanck before.....");callBack();console.log("callBanck after.....");}function callBack(){console.log("callBanck ..........");}writeCode(callBack);</script>//输出结果callBanck before..... test1.html:14callBanck .......... test1.html:21 callBanck after.....
<script>//查找节点var findNodes = function(){var i = 100000,//复杂循环nodes[],//存储本次执行结果found;//零时存储找到的节点while(i){//业务逻辑处理//....found = 1;i -= 1;nodes.push(found);}return nodes;};//对节点处理var hide = function(nodes){var i = 0, max = nodes.length;for(; i < max; i += 1){//复杂业务逻辑处理console.log(nodes[i]);}};//执行函数hide(findNodes());</script>
<script>var findNodes = function(callBack){var i = 10000,nodes = [],dound;//检查回调函数是否为可调用if(typeof callBack != "function"){callBack = false;}while(i){i -= 1;//逻辑处理 。。。。。。。
。
//执行回调函数if(callBack){callBack(found);}nodes.push[found];}return nodes;};var hide = function(node){//对node节点进行相应的业务处理};findNodes(hide);</script>
var scareMe = function(){console.log("FIRST!");scareMe = function(){console.log("SECOND!");};};scareMe();scareMe();输出结果:日志: FIRST! 日志: SECOND!
<script>var scareMe = function(){console.log("FIRST!");scareMe = function(){console.log("SECOND!");};};//加入新的属性scareMe.property = "property";//赋值给还有一个不同名称的变量var prank = scareMe;//在一个方法里使用var spooky = {boo: scareMe};prank();// firstprank();//firstconsole.log(prank.property);//propertyspooky.boo();//firstspooky.boo();//firstconsole.log(spooky.boo.property);//property//使用自己定义函数scareMe();//secondscareMe();//secondconsole.log(scareMe.property);//undefined</script>输出结果:FIRST! test5.html:14 property test5.html:332FIRST! test5.html:14 property test5.html:372SECOND! test5.html:17 undefined
(function(){alert("One");}());
能够全然替代以全局变量形式创建的代码。例如以下代码就不会创建过多的全局变量。
<script>(function(){var days = [‘sun‘, ‘mon‘, ‘tue‘, ‘wed‘, ‘thu‘, ‘fri‘, ‘sat‘],today = new Date(),msg = "Today is " + days[today.getDay()] + "," + today.getDate();alert(msg);}());</script>
(function(who, how){console.log("i‘m " + who + ",I‘m " + how);}("pengke", "shabi"));
var result = (function(){return 2 +2 ;})();
<script> function addPerson(conf){ console.log(conf.first); console.log(conf.second); console.log(conf); } var conf = {first: "abc", second: "a", third: "c"}; addPerson(conf); </script>
<script>//定义函数var sayHi = function(who){console.log("Hello " + (who ?
"," + who : "") + "!");};sayHi();sayHi(‘world‘);sayHi.apply(null, ["world"]);</script>
运行结果Hello ! test10.html:15Hello ,world! test10.html:15Hello ,world!
<script>//curry化的add函数function add(x, y){var oldx = x, oldy = y;if(typeof oldy == "undefined"){return function(newy){console.log(oldx + newy);};}console.log(x + y);}console.log(typeof add(5));//functionadd(3)(4);//7var add2000 = add(2000);add2000(10);//2010</script>
标签:括号 cti white 第一个 结果 自己的 类对象 应用 func
原文地址:http://www.cnblogs.com/jhcelue/p/6962182.html