Last active
December 22, 2015 06:49
-
-
Save zhanhongtao/6433564 to your computer and use it in GitHub Desktop.
函数定义和函数调用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 函数声明 | |
function _name() { | |
//... | |
} | |
// 函数表达式 | |
var _name = function() { | |
//... | |
}; | |
// 基于构造函数 | |
var _name = new Function('return {"a": "a"};'); | |
// 函数参数 | |
// 应用: http://caolanmcmahon.com/posts/flexible_callback_arguments/ | |
_name.length; // 定义时约定参数 - 形参. | |
arguments.length; // 实际调用时, 传递的参数个数 - 实参. | |
/** | |
JavaScript 中, 传递参数时存在值和引用区别. | |
传递引用时, 多个变量指向同一内存. 因此在任何位置修改时, 其它变量同时修改. -> 传说中, 多个变量引用相同地址为了节省内存.... | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 函数调用实例 | |
Array.prototype.slice.call( arguments, 1 ); | |
var value = 120; | |
value = Math.max.call( Math, value, 100 ); | |
value = Math.min.apply( Math, [value, 200] ); | |
// 判断数据类型 | |
function type(s) { | |
return Object.prototype.toString.call(s).toLowerCase().slice(8,-1); | |
} | |
// 指定 context. | |
// Function.prototype.bind | |
function bind( func, context ) { | |
return function() { | |
func.apply( context || this, arguments ); | |
}; | |
) | |
// 预置参数. | |
// 预置参数可放到任意位置. | |
// 参考: http://benalman.com/news/2012/09/partial-application-in-javascript/ | |
function partial( func ) { | |
var args = [].slice.call( arguments, 1 ); | |
return function() { | |
var argus = [].slice.call( arguments ); | |
return func.apply( this, args.concat(argus) ); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// .. | |
_name(); | |
// 方法. | |
window._name(); | |
// call | |
_name.call( context, 'a', 'b' ); | |
// apply | |
_name.apply( context, ['a', 'b'] ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment