简体   繁体   中英

When typeof is run on a function, it returns “function” as the type. Why doesn't it return “object”?

A function in Javascript is an object.

When running typeof on a function, why does it return the value "function", instead of "object"?

 var objLit = {} console.log(typeof objLit); // "object" function hello() { console.log("hello world"); } console.log(typeof hello); // "function"

Because that's what the specification says to do. Although all functions are objects, when using typeof on a function will return 'function' instead of 'object' . See the table :

Type of val                            Result:
Object (does not implement [[Call]])   "object"
Object (implements [[Call]])           "function"

(Functions have the internal method [[Call]]; non-functions do not.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM