函数真的是一个对象 [英] Is Function really an Object

查看:135
本文介绍了函数真的是一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名自学成才的网站开发人员,我仍然试图掌握一些JavaScript基础知识。以下是从Douglas Crockford的Good Parts中摘录的一些引用。



JavaScript中的函数是对象 ,数组是对象,函数是对象,正则表达式是对象,当然,对象是对象。

到一个可以继承属性的原型对象(即构造函数,toString,...)

如果Function是一个对象,那么为什么

  console.log(typeof Function); // function 

是它的类型而不是对象

  console.log(Object.constructor); // Function()

是它的父的构造函数吗

  console.log(Function.constructor); // Function()



困惑所以构造函数在-fact a function?

  console.log(typeof Function.prototype); //函数

是函数的原型而不是对象的类型? 我认为它从Object继承



这些问题的答案将大大帮助我理解JavaScript。 $ b

解决方案


如果Function是一个对象,那么为什么它的类型是一个函数而不是对象?

因为typeof运算符是这样定义的,所以可能为了可用性:


  • 对象(本地和不实现[[Call]])返回object

  • 对象(本机或主机并执行[[Call]])返回function
  • 对象(主机并且不实现[[Call]])
    返回实现定义的值,该值可能不是未定义,布尔,数字或字符串 。



  • [[Call]] 是一个对象的内部属性,将对象标识为函数(可调用)。非本地对象是由主机(例如浏览器)提供的对象,例如DOM对象或ActiveXObject的实例。


    困惑,所以构造函数实际上是一个函数?

    为什么不是?构造函数是函数。实例只能使用函数构造。 Object.constructor 是一个函数,但它也是一个对象。请参阅以下内容:

      console.log((function(){})instanceof Object); 
    // - >真正的

    另外,从ECMAScript speficiation:

    < blockquote>

    每个内置函数和每个内置构造函数都具有Function原型对象,该对象是表达式Function.prototype(15.3.4)的初始值,作为其[[原型]]内部属性。除非另有规定,否则每个内置原型对象都具有Object原型对象,这是Object.prototype表达式的初始值(15.2。 4),作为其[[Prototype]]内部属性的值,除了Object原型对象本身。

    另外,回答你最后的困惑:
    $ b


    Function原型对象本身就是一个Function对象(它的[[Class]]是Function),当被调用时,接受任何参数并返回undefined。



    I am a self taught web developer and am still trying to come to grips with some JavaScript fundamentals. Below are some quotes extracted from Douglas Crockford's Good Parts.

    "Functions in JavaScript are Objects"

    "In JavaScript, arrays are objects, functions are objects, regular expressions are objects, and, of course, objects are objects"

    "Every object is linked to a prototype object from which it can inherit properties" (namely constructor, toString, ...)

    If Function is an Object then why

     console.log(typeof Function);  // function 
    

    is its type a function and not object

     console.log(Object.constructor);  // Function()
    

    is it the constructor of its 'parent'

     console.log(Function.constructor);  // Function()
    

    puzzled so the constructor is in-fact a function?

     console.log(typeof Function.prototype); // Function
    

    is the type of its prototype a function and not an object? i thought it inherited from Object

    Answers to these questions will greatly assist my understanding on JavaScript.

    解决方案

    If Function is an Object then why is its type a function and not object?

    Because the typeof operator is defined like that, probably for usability:

    • Object (native and does not implement [[Call]]) returns "object"
    • Object (native or host and does implement [[Call]]) returns "function"
    • Object (host and does not implement [[Call]]) returns an Implementation-defined value that may not be "undefined", "boolean", "number", or "string".

    [[Call]] is an internal property of an object that identifies the object as a function (callable). A non-native object is an object provided by the host (e.g. browser), such as a DOM object or an instance of ActiveXObject.

    puzzled so the constructor is in-fact a function?

    Why wouldn't it be? Constructors are functions. Instances can only be constructed using functions. Object.constructor is a function, but it's also an object. See the following:

    console.log((function () { }) instanceof Object);
    //-> true
    

    Also, from the ECMAScript speficiation:

    Every built-in function and every built-in constructor has the Function prototype object, which is the initial value of the expression Function.prototype (15.3.4), as the value of its [[Prototype]] internal property.

    Unless otherwise specified every built-in prototype object has the Object prototype object, which is the initial value of the expression Object.prototype (15.2.4), as the value of its [[Prototype]] internal property, except the Object prototype object itself.

    And also, to answer your final puzzlement:

    The Function prototype object is itself a Function object (its [[Class]] is "Function") that, when invoked, accepts any arguments and returns undefined.

    这篇关于函数真的是一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆