javascript笔记--函数

Three ways to create a function:

1.Declarative function: parsed when the javascript application is first loaded.(can place anywhere)

2.Anonymous function or function constructor:parsed each time it's accessed.

3.Function literal or function expression:used in place-such as a callback function.Aslo parsed once

when the javascript application is loaded.can also anonymous.(Place before the function is used)

 

function factorial(n) {
console.log(n);
return n==1 ? 1 : n * factorial(n -1);
}

function noBlock(n, callback){
setTimeout(function() {
var val = factorial(n);
if (callback && typeof callback == "function") {
callback(val);
}
},0);
}

console.log("Top of the morning to you");

noBlock(3, function(n) {
console.log("first call ends with " + n);
noBlock(n, function(m) {
console.log("final result is " + m);
});
});

var tst = 0;
for (i=0; i<10; i++) {
tst+=i;
}

console.log("value of tst is " + tst);

noBlock(4, function(n) {
console.log("end result is " +n);
});
console.log("not doing too much");

 

1.Create a funtion that can remember data, but without having to use global variables and without resending the same data

with each function call.

A way to persist this data from one function to another is to create one of the functions within the other, so both have access  to the data,and then return the inner function from the outer.

when the returned function is using the outer function's scope, is known as a

function closure.

function outer (x) {

  return function(y) {return x * y;};

}

var multiThree = outer(3);

alert(multiThree(2)); alert(multiThree(3));

 

2. Converting function argument into an array

use Array.prototype.slice() and then the function call() method to convert arguments 

collection into an array. 

an array-like object consisting of all arguments passed to the function

convert a NodeList into an array

function sumRounds() {
var args = [].slice.call(arguments);

return args.reduce(function(val1, val2) {
return parseInt(val1, 10) + parseInt(val2, 10);
});
}

var sum = sumRounds("2.3", 4, 5, "16", 18.1);

3.Reducing redundancy by using a partial application

One function perform a process on a given number of arguments and return a

result, while a second function acts as a function factory:churning out functions thet

return the first function, but with arguments already encoded.
function makeString(ldelim, str, rdelim) {
return ldelim + str + rdelim;
}

function quoteString(str) {
return makeString("'", str, "'");
}

function barString(str) {
return makeString("-", str, "-");
}

console.log(quoteString("apple"));
console.log(barString("apple"));

 

console.log(sum);

转载于:https://www.cnblogs.com/sky-zhao/p/5056141.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值