what is the arguments variable an instance of?

后端 未结 2 1882
死守一世寂寞
死守一世寂寞 2021-01-21 18:57

MDN says it is \"an Array like object\" but does not say what it is an instance of.

It is not an HTMLCollection or NodeList.

If I call

相关标签:
2条回答
  • 2021-01-21 19:14

    You can retrieve the name of the function where arguments returned from using callee.name

    function test() {
      this.args = arguments;
    }
    
    var obj = new test();
    
    console.log(obj.args.callee.name);
    

    function test() {
      return arguments
    }
    
    console.log(test().callee.name);
    

    See also Why was the arguments.callee.caller property deprecated in JavaScript? , Arguments object

    0 讨论(0)
  • 2021-01-21 19:15

    So what is arguments an instance of?

    It's an instance of Object. There does not appear to be any public Arguments constructor that you can use with instanceof to identify it that way.

    If you want to uniquely identify it, then:

    Object.prototype.toString.call(arguments) === "[object Arguments]"
    

    is a safe way to identify it.

    Per section 9.4.4 in the EcmaScript 6 specification, the arguments object is either an ordinary object or an exotic object. Here's what the spec says:

    Most ECMAScript functions make an arguments objects available to their code. Depending upon the characteristics of the function definition, its argument object is either an ordinary object or an arguments exotic object.

    An arguments exotic object is an exotic object whose array index properties map to the formal parameters bindings of an invocation of its associated ECMAScript function.

    Arguments exotic objects have the same internal slots as ordinary objects. They also have a [[ParameterMap]] internal slot. Ordinary arguments objects also have a [[ParameterMap]] internal slot whose value is always undefined. For ordinary argument objects the [[ParameterMap]] internal slot is only used by Object.prototype.toString (19.1.3.6) to identify them as such.

    Since it is an "exotic" object, that essentially means it doesn't follow all the normal and expected conventions. For example, there is no constructor function from which you can create your own object. And, because there is no public constructor function, that probably also explains why there's no instanceof that you can test on it to uniquely identify it.

    0 讨论(0)
提交回复
热议问题