为什么函数变量可以有对象属性而数字变量不能? [英] Why does function variable can have object property and number variable not?
本文介绍了为什么函数变量可以有对象属性而数字变量不能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用变量时发现了一件非常神秘的事情.
I found very mysterious thing when I am using variable.
请看下面的片段.
var abc = function(){};
abc.prop = 1;
console.log(abc);
console.log(abc.prop);
var abc = 3;
abc.prop = 1;
console.log(abc);
console.log(abc.prop);
推荐答案
为什么第一个片段的 console.log(abc);
只显示函数体,没有 prop
属性?
因为这就是 console.log()
的定义:如果是函数,就显示它的主体."
Why console.log(abc);
of the first snippet is showing only the function body, without the prop
property?
Because that's what console.log()
is defined to do:
"If it's a function, just display its body."
你可以使用
Object.getOwnPropertyNames(abc)
用于所有属性的名称,Object.keys(abc)
用于可枚举 属性的名称,Object.values(abc)
用于可枚举 属性的值,Object.entries(abc)
用于可枚举 属性的名称和值.
Object.getOwnPropertyNames(abc)
for all properties' names,Object.keys(abc)
for the enumerable properties' names,Object.values(abc)
for the enumerable properties' values,Object.entries(abc)
for both enumerable properties' names and values.
像任何其他Object
一样.
这篇关于为什么函数变量可以有对象属性而数字变量不能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文