資源描述:
《angularjs學習筆記—工具方法 - SegmentFault.pdf》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、2015/9/11angularjs學習筆記—工具方法-SegmentFault文章(/blogs)學習筆記(/blog/yuyang040160120)文章詳情angularjs學習筆記—工具方法(/a/1190000002625738)小俞(/u/yuyang040160120)2.8k3月27日發(fā)布推薦2推薦收藏18收藏,3.1k瀏覽angular.bind(self,fn,args)作用:返回一個新的函數(shù),綁定這個函數(shù)的this指向self參數(shù):self:新函數(shù)的上下文對象fn:需要綁定的函數(shù)args:傳遞給函數(shù)的參數(shù)返回值:this指向self的新函數(shù)v
2、arobj={name:'xxx',print:function(country){console.log(this.name+'isform'+country);}};varself={name:'yyy'};varbindFn=angular.bind(self,obj.print,'China');//varbindFn=angular.bind(self,obj.print,['China']);obj.print('American');//$xxxisformAmericanbindFn();//$yyyisformChina注意:bind會根據(jù)你的參
3、數(shù)類型來決定調(diào)用call或apply,所以args可以是一個個數(shù)據(jù),也可以是一個數(shù)組哦。angular.copy(source,[destination])作用:對象的深拷貝http://segmentfault.com/a/11900000026257381/82015/9/11angularjs學習筆記—工具方法-SegmentFault參數(shù):source:源對象destination:拷貝的對象返回值:拷貝的對象varobj={name:'xxx',age:50};varcopyObj=angular.copy(obj);console.log(copyOb
4、j);//$Object{name:"xxx",age:50}angular.equals(o1,o2)作用:正常比較和對象的深比較參數(shù):o1:比較的對象o2:比較的對象返回值:booleanangular.equals(3,3);//$trueangular.equals(NaN,NaN);//$trueangular.equals({name:'xxx'},{name:'xxx'});//$trueangular.equals({name:'xxx'},{name:'yyy'});//$falseangular.extend(dst,src)作用:對象的拓展參
5、數(shù):dst:拓展的對象src:源對象返回值:拓展的對象vardst={name:'xxx',country:'China'};varsrc={name:'yyy',age:10};angular.extend(dst,src);console.log(src);//$Object{name:"yyy",age:10}console.log(dst);//$Object{name:"yyy",country:"China",age:10}http://segmentfault.com/a/11900000026257382/82015/9/11angularjs學習
6、筆記—工具方法-SegmentFaultangular.forEach(obj,iterator,[context])作用:對象的遍歷參數(shù):obj:對象iterator:迭代函數(shù)context:迭代函數(shù)中上下文返回值:objvarobj={name:'xxx',country:'China'};angular.forEach(obj,function(value,key){console.log(key+':'+value);});//$name:xxx//$country:Chinavararray=['xxx','yyy'];angular.forEach(a
7、rray,function(item,index){console.log(index+':'+item+'form'+this.country);},obj);//$0:xxxformChina//$1:yyyformChinaangular.fromJson(string)作用:字符串轉(zhuǎn)json對象參數(shù):string:字符串返回值:json對象varjson=angular.fromJson('{"name":"xxx","age":34}');console.log(json);//$Object{name:"xxx",age:34}angular.toJs
8、on(js