angularjs學習筆記—工具方法,angularjs學習筆記

來源:互聯網
上載者:User

angularjs學習筆記—工具方法,angularjs學習筆記
angular.bind(self, fn, args)

  • 作用:返回一個新的函數,綁定這個函數的this指向self
  • 參數:

    • self:新函數的內容物件
    • fn:需要綁定的函數
    • args:傳遞給函數的參數
  • 傳回值:this指向self的新函數

    var obj = {    name: 'xxx',    print: function (country) {        console.log(this.name + ' is form ' + country);    }};var self = {    name: 'yyy'};var bindFn = angular.bind(self, obj.print, 'China');//var bindFn = angular.bind(self, obj.print, ['China']);obj.print('American'); //$ xxx is form AmericanbindFn(); //$ yyy is form China

注意:bind會根據你的參數類型來決定調用call或apply,所以args可以是一個個資料,也可以是一個數組哦。

angular.copy(source, [destination])
  • 作用:對象的深拷貝
  • 參數:

    • source:來源物件
    • destination:拷貝的對象
  • 傳回值:拷貝的對象

    var obj = {    name: 'xxx',    age: 50};var copyObj = angular.copy(obj);console.log(copyObj); //$ Object {name: "xxx", age: 50}
angular.equals(o1, o2)
  • 作用:正常比較和對象的深比較
  • 參數:

    • o1:比較的對象
    • o2:比較的對象
  • 傳回值:boolean

    angular.equals(3, 3); //$ trueangular.equals(NaN,NaN); //$ trueangular.equals({name:'xxx'},{name:'xxx'}); //$ trueangular.equals({name:'xxx'},{name:'yyy'}); //$ false
angular.extend(dst, src)
  • 作用:對象的拓展
  • 參數:

    • dst:拓展的對象
    • src:來源物件
  • 傳回值:拓展的對象

    var dst = {name: 'xxx', country: 'China'};var src = {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}
angular.forEach(obj, iterator, [context])
  • 作用:對象的遍曆
  • 參數:

    • obj:對象
    • iterator:迭代函數
    • context:迭代函數中上下文
  • 傳回值:obj

    var obj = {name: 'xxx', country: 'China'};angular.forEach(obj, function (value, key) {    console.log(key + ':' + value);});//$ name:xxx//$ country:Chinavar array = ['xxx', 'yyy'];angular.forEach(array, function (item, index) {    console.log(index + ':' + item + ' form ' + this.country);}, obj);//$ 0:xxx form China//$ 1:yyy form China
angular.fromJson(string)
  • 作用:字串轉json對象
  • 參數:

    • string:字串
  • 傳回值:json對象

    var json = angular.fromJson('{"name":"xxx","age":34}');console.log(json); //$ Object {name: "xxx", age: 34}
angular.toJson(json,pretty)
  • 作用:json對象轉字串
  • 參數:

    • json:json
    • pretty:boolean number 控制字元串輸出格式
  • 傳回值:字串

    angular.toJson({name:'xxx'});//$ "{"name":"xxx"}"angular.toJson({name:'xxx'},true);//$ "{//$    "name": "xxx"//$ }"angular.toJson({name:'xxx'},10);//$ "{//$            "name": "xxx"//$ }"
angular.identity(value)
  • 作用:返回這個函數的第一個參數
  • 參數:

    • value:參數
  • 傳回值:第一個參數

    console.log(angular.identity('xxx','yyy')); //$ xxx
angular.isArray(value)
  • 作用:判斷一個資料是否是數組
  • 參數:

    • value:資料
  • 傳回值:boolean

    angular.isArray(3); //$ falseangular.isArray([]); //$ trueangular.isArray([1, 2, 3]); //$ trueangular.isArray({name: 'xxx'}); //$ false
angular.isDate(value)
  • 作用:判斷一個資料是否是Date類型
  • 參數:

    • value:資料
  • 傳回值:boolean

    angular.isDate('2012-12-02'); //$ falseangular.isDate(new Date()); //$ true
angular.isDefined(value)
  • 作用:判斷一個資料是否是defined類型
  • 參數:

    • value:資料
  • 傳回值:boolean

    angular.isDefined(undefined) //$ falseangular.isDefined([]); //$ true
angular.isUndefined(value)
  • 作用:判斷一個資料是否是undefined類型
  • 參數:

    • value:資料
  • 傳回值:boolean

    angular.isUndefined(undefined) //$ trueangular.isUndefined([]); //$ false
angular.isFunction(value)
  • 作用:判斷一個資料是否是函數
  • 參數:

    • value:資料
  • 傳回值:boolean

    angular.isFunction(function(){}); //$ trueangular.isFunction(3); //$ false
angular.isNumber(value)
  • 作用:判斷一個資料是否是Number類型
  • 參數:

    • value:資料
  • 傳回值:boolean

    angular.isNumber(4); //$ trueangular.isNumber('xxx'); //$ falseangular.isNumber(new Number(4)); //$ falseangular.isNumber(Number(4)); //$ true
angular.isObject(value)
  • 作用:判斷一個資料是否是對象
  • 參數:

    • value:資料
  • 傳回值:boolean

    angular.isObject('xxx'); //$ false      angular.isObject(null); //$ falseangular.isObject([]); //$ trueangular.isObject(function(){}); //$ falseangular.isObject({name:'xxx'}); //$ true
angular.isString(value)
  • 作用:判斷一個資料是否是字串
  • 參數:

    • value:資料
  • 傳回值:boolean

    angular.isString(4); //$ falseangular.isString('xxx'); //$ trueangular.isString(new String('xxx')); //$ falseangular.isString(String('xxx')); //$ true
angular.lowercase(string)
  • 作用:將字串大寫字母變小寫
  • 參數:

    • string:字串
  • 傳回值:改變後的新字串

    var newString = angular.lowercase('XXyyZZ');console.log(newString); //$ xxyyzz
angular.uppercase(string)
  • 作用:將字串小寫字母變大寫
  • 參數:

    • string:字串
  • 傳回值:改變後的新字串

    var newString = angular.uppercase('XXyyZZ');console.log(newString); //$ XXYYZZ
angular.noop()
  • 作用:空函數

    var flag = false;    flag ? console.log('xxx') : angular.noop();

轉載自 http://segmentfault.com/a/1190000002625738

實驗地址 http://www.linchaoqun.com/html/angular/function.jsp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.