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 American
bindFn(); //$ 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); //$ true
angular.equals(NaN,NaN); //$ true
angular.equals({name:'xxx'},{name:'xxx'}); //$ true
angular.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:China var 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); //$ false
angular.isArray([]); //$ true
angular.isArray([1, 2, 3]); //$ true
angular.isArray({name: 'xxx'}); //$ false
angular.isDate(value)
- 作用:判断一个数据是否是Date类型
- 参数:
- value:数据
返回值:boolean
angular.isDate('2012-12-02'); //$ false
angular.isDate(new Date()); //$ true
angular.isDefined(value)
- 作用:判断一个数据是否是defined类型
- 参数:
- value:数据
返回值:boolean
angular.isDefined(undefined) //$ false
angular.isDefined([]); //$ true
angular.isUndefined(value)
- 作用:判断一个数据是否是undefined类型
- 参数:
- value:数据
返回值:boolean
angular.isUndefined(undefined) //$ true
angular.isUndefined([]); //$ false
angular.isFunction(value)
- 作用:判断一个数据是否是函数
- 参数:
- value:数据
返回值:boolean
angular.isFunction(function(){}); //$ true
angular.isFunction(3); //$ false
angular.isNumber(value)
- 作用:判断一个数据是否是Number类型
- 参数:
- value:数据
返回值:boolean
angular.isNumber(4); //$ true
angular.isNumber('xxx'); //$ false
angular.isNumber(new Number(4)); //$ false
angular.isNumber(Number(4)); //$ true
angular.isObject(value)
- 作用:判断一个数据是否是对象
- 参数:
- value:数据
返回值:boolean
angular.isObject('xxx'); //$ false
angular.isObject(null); //$ false
angular.isObject([]); //$ true
angular.isObject(function(){}); //$ false
angular.isObject({name:'xxx'}); //$ true
angular.isString(value)
- 作用:判断一个数据是否是字符串
- 参数:
- value:数据
返回值:boolean
angular.isString(4); //$ false
angular.isString('xxx'); //$ true
angular.isString(new String('xxx')); //$ false
angular.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();
angularjs学习笔记—工具方法的更多相关文章
- vue.js 源代码学习笔记 ----- 工具方法 env
/* @flow */ /* globals MutationObserver */ import { noop } from 'shared/util' // can we use __proto_ ...
- vue.js 源代码学习笔记 ----- 工具方法 option
/* @flow */ import Vue from '../instance/index' import config from '../config' import { warn } from ...
- vue.js 源代码学习笔记 ----- 工具方法 share
/* @flow */ /** * Convert a value to a string that is actually rendered. { .. } [ .. ] 2 => '' */ ...
- vue.js 源代码学习笔记 ----- 工具方法 perf
import { inBrowser } from './env' export let mark export let measure if (process.env.NODE_ENV !== 'p ...
- vue.js 源代码学习笔记 ----- 工具方法 error
import config from '../config' import { warn } from './debug' import { inBrowser } from './env' // 这 ...
- vue.js 源代码学习笔记 ----- 工具方法 props
/* @flow */ import { hasOwn, isObject, isPlainObject, capitalize, hyphenate } from 'shared/util' imp ...
- vue.js 源代码学习笔记 ----- 工具方法 lang
/* @flow */ // Object.freeze 使得这个对象不能增加属性, 修改属性, 这样就保证了这个对象在任何时候都是空的 export const emptyObject = Obje ...
- vue.js 源代码学习笔记 ----- 工具方法 debug
import config from '../config' import { noop } from 'shared/util' let warn = noop let tip = noop let ...
- AngularJS学习笔记2——AngularJS的初始化
本文主要介绍AngularJS的自动初始化以及在必要的适合如何手动初始化. Angular <script> Tag 下面通过一小段代码来介绍推荐的自动初始化过程: <!doctyp ...
随机推荐
- swift 学习(二)基础知识 (函数,闭包,ARC,柯里化,反射)
函数 func x(a:Int, b:Int) {} func x(a:Int, b:Int) -> Void {} func x(a:Int, b:Int) ->(Int,Int ...
- 100200H
这是个bfs 首先建图,先从终点bfs求出每点距离,然后从起点开始,确定初始方向:某点和自己相邻距离比自己小1就是 然后就先贪心和上次一样的方向,如果不能走,就找出一个方向,把自己当前方向改掉,重复过 ...
- jquery-ui-处理拖动位置Droppable,Draggable
一.效果.如下图中,各途中可相互拖拉,右下角可删除.注意图1和图2对比区别 图1 图2 二.源码详解 html源码 <!DOCTYPE html> <html> <hea ...
- Office 2013 Pro Plus Vol激活
先确认自己是office2013 vol(大客户版),然后cmd(管理员)里面运行如下命令: cd "C:\Program Files\Microsoft Office\Office15&q ...
- SVN安装配置和使用教程
注意:location :为安装文件位置,Repositories:为管理的代码仓库的位置,若选中Use secure connection前面的Checkbox,则表示安全连接[https],这里的 ...
- Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现
笔者最近在给客户开发文档管理系统时,客户要求上传到管理系统的文档(包括ppt,word,excel,txt)只能预览不允许下载.笔者想到了百度文库和豆丁网,百度文库和豆丁网的在线预览都是利用flash ...
- Lyaer 单弹出层获取数据
案例完整代码如下 var cls = layer.open({ title: "请选择被换班人", type: 2, ...
- js,jquery转json的几种方法
一.原生js转json, eval()方法,不需要引入外部插件; //由JSON字符串转换为JSON对象 var obj = eval('(' + jsonStr + ')'); 或者 var obj ...
- Irrelevant Elements, ACM/ICPC NEERC 2004, UVa1635
这种题目最重要的是思路了清晰 #include <cstdio> #include <cstring> ;//sqrt(n)+1 is enough ][]; ]; int a ...
- XSD笔记
XML Schema 是基于 XML 的 DTD 替代者. XML Schema 可描述 XML 文档的结构. XML Schema 语言也可作为 XSD(XML Schema Definition) ...