- 善用資源網站搜索包警,以至于找方法的時候不會太過于慌亂業(yè)績束手無策
- MDN庫 撵摆, 菜鳥教程 ,JQ22 害晦,github 特铝,簡書 ,博客
- 各種官網 【webpack壹瘟,Vue鲫剿,node,npm 稻轨,github 灵莲,】
1、函數本身就是一個對象殴俱,可以為其添加靜態(tài)屬性(屬性):
function demo(){
...
}
demo.data={
a:1,
b:2
}
//使用
console.log(demo.data.a);
運行結果:
Paste_Image.png
2政冻、對于有些在函數內部定義的局部變量枚抵,但是需要在其它函數中使用或者外部使用,可以將這些變量賦值給函數的某一個靜態(tài)屬性明场,作為全局變量汽摹,可以供其他地方使用:
// 修改價格
function editPrice(obj){
var productid = $(obj).parent("tr").attr("data-productid");
var productName = $(obj).parent("tr").attr("data-productname");
editPrice.data = {
productid: productid,
productName: productName
...
}
}
$("#editPriceOk").click(function(){
$.ajax({
url:"/v1/xx/xxx/"+editPrice.data.productid, //此處用到全局變量
type:"put",
data:{
"xx":xx,
"xx":xx
},
success:function(){
layer.msg("修改價格成功!",{time:1500});
}
});
});
3苦锨、建立局部作用域保存變量逼泣,點擊彈窗時把值賦給變量
function open(id){
return function(oper){
if(oper == "ok"){
console.log("ok",id);
return ;
}
return console.log("cancel");
}
}
var confirm = open(3);
confirm("ok");
運行結果如下:
Paste_Image.png