开发者

Is Function really an Object

开发者 https://www.devze.com 2023-01-20 06:34 出处:网络
I am a self taught web developer and amstill trying to come to grips with some JavaScript fundamentals. Below are some quotes extracted from Douglas Crockford\'s Good Parts.

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?

开发者_StackOverflow中文版 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.


When we say, "a function is an object", we don't mean "is" as in "instead of", we mean it in the same sense as "a cat is an animal". If someone asked you what kind of pet you had, you wouldn't answer "an animal". typeof would be useless if it always responded object.

A function is an object, but that's not an interesting thing for typeof to return, since it's a static quality of the language itself, not something that needs to be reported at runtime.


The typeof operator would be quite useless if it always returned "object", wouldn't it? Everything is an object, but it can be other things too. For example, a string is an object, but it is also a string :) The operator returns the name of the most specific type so to speak, not the most generic one. That should explain why typeof Functionis "function".

As for the constructor property, the constructor is a function that is invoked by the operator new when an object is created. It is always a function, regardless of whether the object itself is Object, Functionor something else.


The console.log(typeof Function); shows that the object is of type Function and not object.

To give you an example:

function squareValue(var x) {
    return x * x;
}

can be loosely translated as

var squareValue = new Function("x", "return x*x");

and thus, doing

var valueSquared = squareValue(x);

in any of these 2 examples, will produce the same results...

Hence why every function in Javascript is a Function object.

The .constructor, .prototype, .toString on javascript Object/Function are all functions in their respective objects hence why you have the output as "function".

This is based according to the ECMA-262 3rd Edition -December 1999

Hope this helps.


reference Is every JavaScript Object a function?

javascript:alert([ window.navigator.userAgent, Object, Function ].join("\n\n") )

displays

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 
      Ubuntu/10.04 (lucid) Firefox/3.6.3

function Object() {
    [native code]
}

function Function() {
    [native code]
}

also

javascript:alert([ new Object, new Function ].join("\n\n") )

displays

[object Object]

function anonymous() {  }

and

javascript:alert([ new new Function ].join("\n\n") )

displays

[object Object]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号