函数是javascript中的对象? [英] functions are object in javascript?

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

问题描述

var obj = {};
var fn = function(){};
obj.prop = "some value";
fn.prop = "some value";
assert( obj.prop == fn.prop, "Both are objects, both have the property." );
assert(typeof(obj) === 'object', "Yes its an object");
assert(typeof(fn) === 'object', "why is this not an object");

我从周围的人那里听到函数是对象这是我到目前为止所相信的,但为什么第一个条件传递以及第三个失败

I heard from some people around that functions are objects and this is what i am believing so far, but why is the first condition passes well and third one fails.

推荐答案

那是因为 direct 类型的函数是function

That's because the direct type of a function is "function".

然而,你错过了这个断言:

However, you missed this assertion:

fn instanceof Object // true

顺便说一句,类型如 numberstring严格来说是不是 对象的后代,即使他们有喜欢对象,因为他们有方法;只是其中一个让JavaScript变得有趣的事情:)

Btw, types such as "number" and "string" are strictly not descendants of Object, even though they are like objects in the sense that they have methods; just one of those things that makes JavaScript interesting :)

参见: typeof 及其值范围。

See also: typeof and its range of values.

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

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