• 你不知道的arguments


    arguments.callee.caller.arguments[0];

    你知道这行代码是什么意思吗?

    一开始看到我也是懵逼的,这是什么鬼?

    然后就查了下资料,写了个demo,运行了下:

    function foo(a){
        console.log("arg 1>>: ", arguments);
        console.log("arg 2>>: ", arguments.callee);
        console.log("arg 3>>: ", arguments.callee.caller);
        console.log("arg 4>>: ", arguments.callee.caller.arguments);
    }
    
    function bar(b){
        console.log("arguments >>: ", arguments);
        foo(2)
    }
    bar(1)
    
    输出如下:
    arguments >>:  [Arguments] { '0': 1 }
    arg 1>>:  [Arguments] { '0': 2 }
    arg 2>>:  function foo(a){
        console.log("arg 1>>: ", arguments);
        console.log("arg 2>>: ", arguments.callee);
        console.log("arg 3>>: ", arguments.callee.caller);
        console.log("arg 4>>: ", arguments.callee.caller.arguments);
    }
    arg 3>>:  function bar(b){
        console.log("arguments >>: ", arguments);
        foo(2)
    }
    arg 4>>:  [Arguments] { '0': 1 }

    可以看出:

      1.arguments就是调用者传入的参数集合,注意这个集合并不是数组二十一个类数组,可以通过Array.from(arguments)转换成数组,

      2.arguments.callee就是当前函数方法

      3.arguments.callee.caller就是当前函数调用者

      4.arguments.callee.caller.arguments就是当前函数调用者的参数集合

    看了mozilla上提到箭头函数,然后又写了几行试试:

    let foo = (a) => {
        console.log("arg 1>>: ", arguments);
        console.log("arg 2>>: ", arguments.callee);
        console.log("arg 3>>: ", arguments.callee.caller);
        console.log("arg 4>>: ", arguments.callee.caller.arguments);
    }
    
    let bar = (b)=>{
        console.log("arguments >>: ", arguments);
        foo(2)
    }
    bar(1)
    
    输出如下:
    arguments >>:  [Arguments] {
      '0': {},
      '1':
       { [Function: require]
         resolve: { [Function: resolve] paths: [Function: paths] },
         main:
          Module {
            id: '.',
            exports: {},
            parent: null,
            filename: 'd:\docs\demo\day1126\tempCodeRunnerFile.js',
            loaded: false,
            children: [],
            paths: [Array] },
         extensions:
          [Object: null prototype] { '.js': [Function], '.json': [Function], '.node': [Function] },
         cache:
          [Object: null prototype] { 'd:\docs\demo\day1126\tempCodeRunnerFile.js': [Module] } },
      '2':
       Module {
         id: '.',
         exports: {},
         parent: null,
         filename: 'd:\docs\demo\day1126\tempCodeRunnerFile.js',
         loaded: false,
         children: [],
         paths:
          [ 'd:\docs\demo\day1126\node_modules',
            'd:\docs\demo\node_modules',
            'd:\docs\node_modules',
            'd:\node_modules' ] },
      '3': 'd:\docs\demo\day1126\tempCodeRunnerFile.js',
      '4': 'd:\docs\demo\day1126' }
    arg 1>>:  [Arguments] {
      '0': {},
      '1':
       { [Function: require]
         resolve: { [Function: resolve] paths: [Function: paths] },
         main:
          Module {
            id: '.',
            exports: {},
            parent: null,
            filename: 'd:\docs\demo\day1126\tempCodeRunnerFile.js',
            loaded: false,
            children: [],
            paths: [Array] },
         extensions:
          [Object: null prototype] { '.js': [Function], '.json': [Function], '.node': [Function] },
         cache:
          [Object: null prototype] { 'd:\docs\demo\day1126\tempCodeRunnerFile.js': [Module] } },
      '2':
       Module {
         id: '.',
         exports: {},
         parent: null,
         filename: 'd:\docs\demo\day1126\tempCodeRunnerFile.js',
         loaded: false,
         children: [],
         paths:
          [ 'd:\docs\demo\day1126\node_modules',
            'd:\docs\demo\node_modules',
            'd:\docs\node_modules',
            'd:\node_modules' ] },
      '3': 'd:\docs\demo\day1126\tempCodeRunnerFile.js',
      '4': 'd:\docs\demo\day1126' }
    arg 2>>:  function (exports, require, module, __filename, __dirname) {
    let foo = (a) => {
        console.log("arg 1>>: ", arguments);
        console.log("arg 2>>: ", arguments.callee);
        console.log("arg 3>>: ", arguments.callee.caller);
        console.log("arg 4>>: ", arguments.callee.caller.arguments);
    }
    
    let bar = (b)=>{
        console.log("arguments >>: ", arguments);
        foo(2)
    }
    bar(1)
    }
    arg 3>>:  null
    d:docsdemoday1126	empCodeRunnerFile.js:5
        console.log("arg 4>>: ", arguments.callee.caller.arguments);
                                                         ^
    
    TypeError: Cannot read property 'arguments' of null
        at foo (d:docsdemoday1126	empCodeRunnerFile.js:5:54)
        at bar (d:docsdemoday1126	empCodeRunnerFile.js:10:5)
        at Object.<anonymous> (d:docsdemoday1126	empCodeRunnerFile.js:12:1)
        at Module._compile (internal/modules/cjs/loader.js:776:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
        at Module.load (internal/modules/cjs/loader.js:653:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
        at Function.Module._load (internal/modules/cjs/loader.js:585:3)
        at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
        at startup (internal/bootstrap/node.js:283:19)

    不太一样!

    over!

  • 相关阅读:
    pipeline流水线语法格式
    nexus私服配置npm、nuget、pypi
    正则表达式-grep
    awk 经典案例
    nginx安装,配置,及高可用
    git remote add origin错误
    [转]Git 撤销操作
    [转]git命令之git remote的用法
    [转]git学习------>git-rev-parse命令初识
    转 gerrit
  • 原文地址:https://www.cnblogs.com/rainbowLover/p/14044430.html
Copyright © 2020-2023  润新知