对象和功能非常混乱 [英] Object and Function are quite confusing

查看:132
本文介绍了对象和功能非常混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

所以如果Function是一个Object而Object是一个函数怎么来

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

功能===对象功能==对象是假的吗?

我明白检查对象的实例与比较不一样。所以这里的问题是在两个对象(实际上是类型)是彼此的实例的情况下的模糊性,不应该类型相同吗?

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?

注意:Object不是Number或Array的实例,只是Function的一个实例。

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

推荐答案

来自 JavaScript Prototypal Inheritance


在JavaScript中,所有东西都继承自Object。我们可以说
,Object是超类,或者更好,超级构造函数,
每个变量,并且一切都是Object的实例。 Object
构造函数是一个Function,但Function是Objectof的instance。
这意味着这些断言始终为真:

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 功能)===(函数instanceof Object)

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

以上示例为true,因为Object是构造函数,JavaScript中的构造函数始终是Function。在相同的
时间内,每个Function都有自己的原型,而原型总是
从Object.prototype开始继承。 Function
构造函数是一个Function本身,函数原型是
函数(){};

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)

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

这篇关于对象和功能非常混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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