js温故而知新—Function

来源:互联网 发布:板房保暖 知乎 编辑:程序博客网 时间:2024/04/29 06:05
  1. 函数内部属性和方法
function factorial(num) {    if(num<=1){        return 1;    }else{        return num*arguments.callee(num-1);//arguments 的主要用途第保存函数参数,这个对象还有个callee属性,该属性是个指针,指向拥有这个arguments对象的函数    }}console.log(factorial(5));function outer(){    inner();}function inner(){    console.log(arguments.callee.caller);//保存着调用当前函数的函数的引用}outer();// 输出outer函数的定义

2.函数属性和方法

//函数是对象,因此函数也有属性和方法,每个函数包括两个属性:length,prototype(function sayName(name){    console.log(arguments.callee.length);})();// 每个函数都包含两个非继承而来的方法:apply()call()。这两个方法的用途都是在特定的作用域中调用函数,实际上等于函数体内this对象的值//    apply()方法接收两个参数:一个是在其中运行函数的作用域,另一个是参数数组。其中第二个参数可以使Array的实例,也可以是arguments对象function sum(num1,num2){    return num1+num2;}function callSum1(num1,num2) {    return sum.apply(this,arguments);}console.log(callSum1(10,20));function callSum2(num1,num2) {   return sum.apply(this,[num1,num2]);}console.log(callSum2(1,2));var color="red";var o= {    color: "blue"}var showColor=function () {    console.log(this.color);}showColor.apply(this);showColor.call(o);
原创粉丝点击