一、函数的内部属性 ------ 2019-06-03

一、函数内部有两个特殊的对象:arguments和this
1、arguments:它是一个类数组对象,包含着传入函数中的所有参数。
例子:

 function test() {
    console.log(arguments)
 }
  test(1,2,3)

结果:


8436665-7d0794b9d82a8eee.png
image.png

2、arguments对象中有一个callee属性,callee属性是一个指针,指向拥有这个arguments对象的函数;也就是指向arguments对象的爸爸;
例子:

   function test() {
      console.log(arguments.callee)
    }
    test()

结果:


8436665-316057bf65b241aa.png
image.png

注意:在严格模式下访问arguments.callee会报错;

3、caller属性:这个属性保存的是调用当前函数的函数的引用;

function outer(){ 
 inner(); 
} 
function inner(){ 
 alert(inner.caller); 
} 
outer();

执行outer函数后,弹出的是outer函数的代码,也就是inner函数的caller属性保存的是调用inner函数的outer函数的代码;


8436665-cf7c4f24e325ce82.png
image.png

注意:严格模式下,不能给函数的caller属性赋值,否则会报错;

转载于:https://www.jianshu.com/p/76467bcfd10a

猜你喜欢

转载自blog.csdn.net/weixin_34383618/article/details/91168854
今日推荐