Javascript 笔记(1)----函数
??You can pass an anonymous function as a parameter to another function. The?receiving function?can do something useful with the function that you pass.
? You can define an anonymous function and execute it right away.
- Callback Functions
Advantages:
- They let you pass functions withot the need to name them(which means there are less global variables)更少的全部變量
- You can delegate the responsibility of calling a function to another function(which means there is less code to write)你能指派一個回調(diào)函數(shù)給另一個函數(shù),意味著你可以寫更少的代碼
- they can help with performance
function multiplyByTwo(a, b, c) {
var i, ar = [];
for(i = 0; i < 3; i++) {
ar[i] = arguments[i] * 2;
}
return ar;
}
function addOne(a) {
return a + 1;
}
>>>var myarr = [];
>>>myarr = multiplyByTwo(10.20,30);
[20,40,60]
>>for(var i=0; i<3; i++){myarr[i] = addNoe(myarr[i]);}
myarr
[21,41,61]
Loops can be expensive if they go through a lot?or repetitions
匿名函數(shù)的改進:
>>>myarr = mulitiplybyTwo(1,2,3,function(a){return a+1;});
[4,6,8]
- Slef-invoking Functions:calling this functinon right after it was defined.
()();
(
? ? function() {
? ? ? ? alert('hello');
? ?}
)();
you simply place?an anonymous function definition inside parentheses(括號) followed by another set of?parentheses. The second set basically says "execute now" and is also the place to put?any parameters that your anonymous function might accept.
?
One good reason for using self-invoking anonymous functions is to have some?work done?without creating global variables.?A drawback(缺陷), of course, is that you?cannot execute the same function twice (unless you put it inside a loop or another?function). This makes the anonymous self-invoking functions?best suited for?one-off(一次性)?or initialization tasks.
- Inner (Private) Functions
?Bearing in mind that a function is just like any other value.
function a() {
? ? ?function b() {......}
}
a() is gloable function, and then b() is a local function, which canot accessible outside a(); likes a private function
Advantage
-
- You keep the global namespace clean (smaller chance of naming collisions).減少命名空間的沖突
- Privacy—you expose(暴露)?only the functions you decide?to the "outside world"(外部接口),keeping to yourself functionality that is not meant to be consumed(消耗) by the rest?of the application.(讓自己的功能不至于被外界的程序或則應(yīng)用猜測到)
- Function that Return Functions
前面說過,指明return的時候函數(shù)會返回相應(yīng)的值/或則數(shù)組.當沒指明時通常是undefined.同樣,我們可以返回一個函數(shù)!
function a(){
? ? alert('A');
? ? return function () {
? ? ? ? alert('B');
? ? };
}
>>>var newFunc = a();
>>>>newFunc();
//A
//B
若想立即執(zhí)行返回的函數(shù),還可以這么做
>>>a()();
- Function,Rewrite Thyself(你自己)!
Countiuing with the previous example:
>>>a = a();
//A ? (consider this as being the one-off preparatory work)
//B ? (Redefine the global variable a,assigning a new function to it)
This is usefull when a function has?some initial one-off woek to do, The function overwrites itself after the first it back to the function.
var a = function () { ?//最重要的,a在執(zhí)行一次后就變樣了,便成了a -> actualWork();
? ? function someSetup() { //private
? ? ? ? var setup = 'done';
? ? }
? ? function actualWork() { //private
? ? ? ? alert('Work-worky');
? ? }
? ?someSetup(); //調(diào)用私有函數(shù)
? ? return actualWork; ?//return actualWork();區(qū)別在于前者返回的是一個函數(shù),后者返回的是函數(shù)執(zhí)行的結(jié)果
}(); //self-invoking function ?only excute once
好處:在解決瀏覽器的兼容性的問題中,根據(jù)不同的瀏覽器選擇不同的環(huán)境,就像上面一樣,在執(zhí)行一次后a就變了.
轉(zhuǎn)載于:https://www.cnblogs.com/Poised-flw/archive/2013/03/29/2999673.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Javascript 笔记(1)----函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓平台下的音视频即时通讯应用的开发
- 下一篇: java设计模式_模版模式