5.8 functions
5.8.1 语法:
_.functions(obj);
5.8.2 说明:
functions对obj参数进行处理,返回一个数组,数组中包含obj参数中所有方法的属性名,且进行sort()排序
5.8.3 代码示例:
var result;
// 操作数组
result = _.functions([function () { }, 1, function () { }]);
console.log(result); //=> ["0", "2"]
// 操作对象
result = _.functions({ one: function () { }, two: 1});
console.log(result); //=> ["one"]
// 操作函数
var Child = function () { };
Child.prototype = {
name: 'zhangsan',
fun: function () { }
}
result = _.functions(new Child);
console.log(result); //=> ["fun"]
5.8.4 如果obj参数非数组,对象,函数呢?
var result;
// 操作数字
result = _.functions(123);
console.log(result); //=> []
// 操作字符串
result = _.functions('abc');
console.log(result); //=> []
// 操作bool值
result = _.functions(true);
console.log(result); //=> []
2.8.5 _.functions的功能和_.methods是一样的
var result = _.methods({ one: function () { }, two: 1 });
console.log(result); //=> ["one"]
5.8.6 操作window对象(将返回有效的属性名)
var result = _.functions(window);
console.log(result); //=> ["_", "addEventListener", "alert", "atob", "blur", "btoa", "cancelAnimationFrame", "captureEvents", "clearInterval", "clearTimeout", "close", "confirm", "dispatchEvent", "fetch", "find", "focus", "getComputedStyle", "getMatchedCSSRules", "getSelection", "matchMedia", "moveBy", "moveTo", "open", "openDatabase", "postMessage", "print", "prompt", "releaseEvents", "removeEventListener", "requestAnimationFrame", "resizeBy", "resizeTo", "scroll", "scrollBy", "scrollTo", "setInterval", "setTimeout", "stop", "webkitCancelAnimationFrame", "webkitCancelRequestAnimationFrame", "webkitRequestAnimationFrame", "webkitRequestFileSystem", "webkitResolveLocalFileSystemURL"]
5.8.7 使用sort()进行排序
var Child = function () { }, result;
Child.prototype = {
name: 'zhangsan',
fun: function () { },
abc: function () { }
}
result = _.functions(new Child);
console.log(result); //=> ["abc", "fun"]
5.8.8 eval方法
var result = _.functions(eval("[function () {}]"));
console.log(result); //=> ["0"]