遞歸算法應(yīng)用場景:http://www.cnblogs.com/handsCool/p/4496495.html
- 遞歸需要邊界條件,遞歸前進(jìn)段帮哈,遞歸返回段
- 遞歸條件不成立 ==》 遞歸前進(jìn)
- 遞歸條件滿足 ==》 遞歸返回
循環(huán) 實(shí)現(xiàn)階乘 和 遞歸 實(shí)現(xiàn) 一個(gè)版本出來 - 階乘:輸入 n! = n * (n-1) * (n-2) ..... 1
function factorial(num){
if(num<1){
return 1;
}else{
return num*factorial(num-1);
}
}
- n階樓梯,小張每次只能走一階或者兩階,請問走完此樓梯共有多少種方法?
function fun(s){
if(s==0 || s==1){
return 1;
}else{
return fun(s-1)+fun(s-2);
}
}
console.log(fun(4));
<select class="agt_rt_nm_ipt province" style="width: 270px" name="provinceId" data-provinceid="{{provinceId}}"></select>
<select class="agt_rt_nm_ipt city" style="width: 270px" name="cityId" data-cityid="{{cityId}}"></select>
<select class="agt_rt_nm_ipt district" style="width: 270px" name="areaId" data-areaid="{{areaId}}"></select>
<select class="agt_rt_nm_ipt street" style="width: 270px" name="streetsId" data-streestid="{{streetsId}}"></select>
function areaSelect(province,city,district,street){
var html = "<option>省</option>";
$(city).append("<option>市</option>");
$(district).append("<option>區(qū)</option>");
$(street).append("<option>街道</option>");
for(var i = 0;i<areaList.length;i++){
var pp = areaList[i];
html += "<option value='"+pp['id']+"' code='"+pp['code']+"' parentid='"+pp['parentId']+"'>"+pp['name']+"</option>";
}
$(province).html(html);
$(province).change(function(){
if($(this).val()=="") return;
$(city+' option').remove();
$(district+' option').remove();
$(street+' option').remove();
var code = $(this).find('option:selected').attr("code");
var html = "<option>市</option>";
for(var i = 0;i<areaList.length;i++){
for(var j = 0;j<areaList[i]['subordinates'].length;j++){
var cc = areaList[i]['subordinates'][j];
if(cc['parentId'] == code){
html += "<option value='"+cc['id']+"' code='"+cc['code']+"' parentid='"+cc['parentId']+"'>"+cc['name']+"</option>";
}
}
}
$(city).html(html);
});
$(city).change(function(){
if($(this).val()=="") return;
$(district+' option').remove();
$(street+' option').remove();
var code = $(this).find('option:selected').attr("code");
var html = "<option>區(qū)</option>";
for(var i = 0;i<areaList.length;i++){
for(var j = 0;j<areaList[i]['subordinates'].length;j++){
for(var k = 0;k<areaList[i]['subordinates'][j]['subordinates'].length;k++){
var dd = areaList[i]['subordinates'][j]['subordinates'][k];
if(dd['parentId'] == code){
html += "<option value='"+dd['id']+"' code='"+dd['code']+"' parentid='"+dd['parentId']+"'>"+dd['name']+"</option>";
}
}
}
}
$(district).html(html);
});
$(district).change(function(){
if($(this).val()=="") return;
$(street+' option').remove();
var code = $(this).find('option:selected').attr("code");
var html = "<option>街道</option>";
for(var i = 0;i<areaList.length;i++){
for(var j = 0;j<areaList[i]['subordinates'].length;j++){
for(var k = 0;k<areaList[i]['subordinates'][j]['subordinates'].length;k++){
for(var l = 0;l<areaList[i]['subordinates'][j]['subordinates'][k]['subordinates'].length;l++){
var tt = areaList[i]['subordinates'][j]['subordinates'][k]['subordinates'][l];
if(tt['parentId'] == code){
html += "<option value='"+tt['id']+"' code='"+tt['code']+"' parentid='"+tt['parentId']+"'>"+tt['name']+"</option>";
}
}
}
}
}
$(street).html(html);
});
}
areaSelect(".province",".city",".district",".street");
function selectOption(selector){
var _selectorId = $("select[name="+selector+"]").attr("data-"+selector);
var options = $("select[name="+selector+"]").children("option");
for(var i =0;i<options.length;i++){
if(options[i].value == _selectorId){
options[i].selected = true;
}
}
}
selectOption("provinceId");
selectOption("cityId");
selectOption("areaId");
selectOption("streetsId");
閉包【closure】
應(yīng)用場景
- 閉包常用來實(shí)現(xiàn)對象的私有數(shù)據(jù)膛檀,在事件處理和回調(diào)函數(shù)中會用到
- 偏函數(shù),柯里化娘侍,函數(shù)式編程等
什么是閉包
- 閉包能讓我們從一個(gè)函數(shù)內(nèi)部訪問其外部函數(shù)的作用域【內(nèi)部函數(shù)能訪問外部函數(shù)作用域中的變量】
- 在JS中咖刃,閉包是一種用來實(shí)現(xiàn)數(shù)據(jù)私有的原生機(jī)制,當(dāng)使用閉包來實(shí)現(xiàn)數(shù)據(jù)私有時(shí)憾筏,被封裝的變量只有在閉包容器函數(shù)作用域中使用嚎杨。在閉包作用域下的公開方法才可以訪問這些數(shù)據(jù)。
- 閉包通常是在一個(gè)函數(shù)內(nèi)部創(chuàng)建另一個(gè)函數(shù)
- 閉包:一個(gè)函數(shù)可以訪問不在其作用域范圍內(nèi)但在其外層作用域中存在的變量氧腰,該外層作用域的頂層為全局作用域
- 閉包不等于匿名函數(shù)
- 閉包的目的是通過返回函數(shù)來擴(kuò)大作用域