關(guān)于函數(shù)的基礎(chǔ)知識(shí)以及一些案例的總結(jié)
<!DOCTYPE?html>
<html?lang="en">
<head>
<meta?charset="UTF-8">
<meta?name="viewport"?content="width=device-width, initial-scale=1.0">
<meta?http-equiv="X-UA-Compatible"?content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
<script>
//函數(shù)
// 概念:由一堆代碼組成,實(shí)現(xiàn)一些特定的功能,通過函數(shù)自身和一些事件實(shí)現(xiàn)
//函數(shù)的創(chuàng)建
// 1,聲明式
// function fn(){}
// function:聲明函數(shù)的關(guān)鍵字
// fn:函數(shù)名
// ():存放函數(shù)的形參
// {}函數(shù)體,函數(shù)的執(zhí)行語句
// fn() 函數(shù)的執(zhí)行
// 難點(diǎn):回調(diào)函數(shù)
// function fn(a){
// console.log(a);
// a(function(c){
// console.log("hello")
// c();
// })
// }
// fn(function(b){
// console.log(b);
// b(function(){
// console.log("腦袋疼")
// })
// })
// fn()將function(b){console.log(b)}作為實(shí)參傳給了函數(shù)fn 此時(shí)(a)接受到的相當(dāng)于a=function.log(b){console.log(b)}
// 這就相當(dāng)于一個(gè)新的函數(shù)a;此時(shí)執(zhí)行a()相當(dāng)于執(zhí)行函數(shù)a,如果實(shí)參a()里面還是一個(gè)新的函數(shù),那個(gè)就相當(dāng)于創(chuàng)建了一個(gè)新的函數(shù)b,就是b=function(c){console.log("hello")}
// 這樣子執(zhí)行b()執(zhí)行了函數(shù)b 得到了hello的值
// 函數(shù)的實(shí)參與形參的關(guān)系
// 1,實(shí)參與形參之間一一對(duì)應(yīng)
// 2,當(dāng)實(shí)參多余形參時(shí),實(shí)參存放在arguments中
// 3,當(dāng)形參多余實(shí)參時(shí),多余的形參為underfind,因?yàn)檫@些形參聲明了但未經(jīng)過實(shí)參的賦值
// arguments的用法:
// 定義:arguments是函數(shù)中的一個(gè)數(shù)組,專門用來存放函數(shù)中的實(shí)參,只有在函數(shù)中才能獲取
// demo:
// 1.任意個(gè)數(shù)字的和
// function fn(){
// var sum=0;
// for(var i=0;i<arguments.length;i++){ //注意:這里的i代表的不是數(shù)組,而是由arguments 數(shù)組的序列號(hào) 由0 開始到arguments.length-1;
// sum+=arguments[i]
// }
// console.log(sum);
// }
// fn(3,2,34,36,242);
// demo:
// 1.讓三個(gè)不同大小的數(shù)字,按照由大到小的順序依次打印出
// function fn(a,b,c){
// if(a>b&&a>c){
// if(b>c){
// console.log(a,b,c);
// }else{
// console.log(a,c,b);
// }
// }else if(b>a&&b>c){
// if(a>c){
// console.log(b,a,c);
// }else{
// console.log(b,c,a);
// }
// }else if(c>a&&c>b){
// if(a>b){
// console.log(c,a,b);
// }else{
// console.log(c,b,a);
// }
// }
// }
// fn(7,4,6)
// 作用域:變量生效的區(qū)域
// 作用域的分類:
// 全局:整個(gè)代碼的區(qū)域
// 局部:一個(gè)函數(shù)就是一個(gè)局部
// 全局作用域下的變量叫做全局變量
// 局部作用域下的變量叫做局部變量
// 函數(shù)的形參也是一個(gè)變量,函數(shù)是一個(gè)局部作用域,所以形參是一個(gè)局部變量
// 變量的生命周期:
// 全局變量:一直存在,隨時(shí)可以被取用
// 局部變量:隨著函數(shù)的運(yùn)行開始而開始,函數(shù)的結(jié)束而結(jié)束
// 提升:
// 1,變量的提升;使用var聲明的變量,會(huì)提升到當(dāng)前作用域開始的位置,被聲明,在原位賦值
// 2,函數(shù)的提升:只要使用function聲明的函數(shù),都會(huì)整體提升,在當(dāng)前作用域內(nèi)都可以被使用
// 但是使用賦值式創(chuàng)建的函數(shù),提升的是聲明,而不是函數(shù)
// 當(dāng)函數(shù)名與變量同名時(shí),變量會(huì)在函數(shù)上面的位置,導(dǎo)致函數(shù)生效;
function?fn(){
console.log(a); //f2
var?a = "hello";
console.log(a); //"hello"
function?a(){1}
console.log(a); //"hello"
a = "world";
console.log(a); //"world"
function?a(){2}
console.log(a); //"world"
}
// 以上的式子相當(dāng)于
// function fn(){
// var a;
// function a(){1}
// function a(){2}
// console.log(a); //f2
// a = "hello";
// console.log(a); //"hello"
// console.log(a); //"hello"
// a = "world";
// console.log(a); //"world"
// console.log(a); //"world"
// }
// fn();
// 重點(diǎn)??!函數(shù)的遞歸:
// 遞歸實(shí)質(zhì)上就是一個(gè)死循環(huán),重要的是他的停止條件,這時(shí)就需要return來幫助函數(shù)終止運(yùn)行
// demo:
// 1.計(jì)算5的階乘
// function fn(n){
// if(n==1){
// return 1;
// }else{
// return n*fn(n-1);
// }
// }
// console.log(fn(3));
// 2.用遞歸計(jì)算1,1,2,3,5,8,13....
// function fn(n){
// if(n==1||n==2){
// return 1;
// }else{
// return fn(n-2)+fn(n-1);
// }
// }
// console.log(fn(3));
// 3.求一個(gè)數(shù)的最大公約數(shù)
// function fn(m,n){
// var r=m%n;
// m=n;
// n=r;
// if(r==0){
// return m;
// }else{
// return fn(m,n);
// }
// }
// console.log(fn(24,6));
// 遞歸的總結(jié),遞歸是一個(gè)遞和歸的過程,在歸的過程中,判斷遞的條件,不成立的話將fn(m,n)再進(jìn)入到函數(shù)中
// 執(zhí)行一次;
// 遞歸實(shí)質(zhì)上就是一個(gè)死循環(huán)
// 遞歸的樣式如下
// function fn(){
// fn();
// }
// fn();
// 所以說,讓遞歸停止的條件很重要
// 構(gòu)造函數(shù):
// 構(gòu)造函數(shù)是一種函數(shù)的存儲(chǔ)方式
// 數(shù)據(jù)的存儲(chǔ)方式:
// 1,字面量 var a=123;
// 2,構(gòu)造函數(shù),var b=new Number(123);
// 重點(diǎn) 對(duì)象:對(duì)象是對(duì)事物的描述
// 對(duì)象的作用是存儲(chǔ)數(shù)據(jù)
// 對(duì)象的實(shí)質(zhì)就是鍵值對(duì)(屬性:屬性值)
// 鍵值對(duì)之間用,隔開
// 對(duì)象中的屬性叫做屬性
// 對(duì)象中的函數(shù)叫做方法名
// 語法:對(duì)象.屬性(方法名)
</script>
</html>