Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 计算对象的长度_Javascript - Fatal编程技术网

Javascript 计算对象的长度

Javascript 计算对象的长度,javascript,Javascript,如何计算对象的长度 console.log({0:"a",1:"b"}.length)//returns undefined but object has the length property Object.hasOwnProperty("length") // true 但是hasOwnProperty()如何工作呢?Object没有这个方法,这个方法在它的原型中你可以使用 Object.keys({0:"a",1:"b"}).length; Object.keys(obj)

如何计算对象的长度

console.log({0:"a",1:"b"}.length)//returns undefined but object has the length property


Object.hasOwnProperty("length") // true
但是hasOwnProperty()如何工作呢?Object没有这个方法,这个方法在它的原型中

你可以使用

Object.keys({0:"a",1:"b"}).length;    

Object.keys(obj).length
FYI,
Object.hasOwnProperty(“length”)
返回
true
,因为
Object
是一个函数。您创建的对象没有
.length
属性,也没有继承属性(
object.prototype.hasOwnProperty('length')==false
)。@elclars不,先生,我不想这样,我想在这里使用length属性{}。length@Felix克林非常感谢,我被名称对象弄糊涂了,忘记了它也是一个function@Maizere:
{}
没有
.length
<代码>对象(构造函数)具有。请尝试
console.log({0:a',1:b”}.hasOwnProperty(“长度”))