1. 函数的创建方式
1.1 静态方法
1 2
| function add(a,b){return a + b;} var add = function(a,b){return a + b;}
|
1.2 动态方法
1
| var add = new Function("a","b","return a + b;");
|
2. 函数参数及arguments
1 2 3 4
| function add(a,b,c){ console.log(add.length); console.log(arguments.length); }
|
arguments:1.仅在函数里面有。2.专为函数参数设计的对象。3.伪数组。4.callee(也是参数,它是指向函数本身自己)
1 2 3 4 5 6 7 8
| var x = function(a,b,c){ arguments.callee.length; }
function add(a,b,c){ add.length; }
|
3. 函数的三大特性
- 方法特性() 运行大括号包裹的代码
- 对象特性 . 获取对象的属性
- 类的特性 new 创建类似对象
4. 函数的call、apply、bind
4.1 call、apply
call、apply定义:把函数临时赋值到对象上面并执行。1 2 3 4
| function.call(obj,args1,args2)
function.apply(obj,args)
|
两者区别:传递参数的形式不一样。
apply参数是数组,也可以是一个伪数组,只要能用下标访问到就可以。
4.2 bind
bind定义:把函数拷贝一份,并插入到对象作用域上面。语法:
1
| obj.e = function.bind(obj,args1,args2)
|