Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

javascript - Object and Function are quite confusing

Object instanceof Object
true
Object instanceof Function
true
Function instanceof Object
true
Function instanceof Function
true

so if Function is an Object and the Object is a Function how come

Function === Object and Function == Object are false?

I do understand that checking the instance of an object is not the same as comparison. So the question here is the fuzziness in the case where if two objects (which are actually types) are instances of each other, shouldn't the types be the same?

Note: Object is not an instance of a Number or an Array just an instance of Function.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From JavaScript Prototypal Inheritance:

Quite everything, in JavaScript, inherits from Object. We could say that Object is the super class, or better, the super constructor, of every variable and that everything is an instanceof Object. The Object constructor is a Function, but a Function is an instanceof Object. This means that these assertions are always true:

(Object instanceof Function) === (Function instanceof Object)

Above example is true because Object is a constructor, and a constructor in JavaScript is always a Function. At the same time, every Function has its own prototype, and a prototype always starts its inheritance from Object.prototype. The Function constructor, is a Function itself, and the function prototype is a function(){};

(Function.prototype instanceof Object) === (function(){} instanceof Object)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...