admin管理员组文章数量:1022496
I am trying to understand something in JavaScript.
Lets say I do the following:
let x = [];
x['Monday'] = 'Work';
x['Tuesday'] = 'More Work';
console.log(x) //[Monday: "Work", Tuesday: "More Work"]
console.log(x.Monday) //"Work"
console.log(x.Tuesday) //"More Work"
Can someone help explain why the array now behaves like an Object?
I am trying to understand something in JavaScript.
Lets say I do the following:
let x = [];
x['Monday'] = 'Work';
x['Tuesday'] = 'More Work';
console.log(x) //[Monday: "Work", Tuesday: "More Work"]
console.log(x.Monday) //"Work"
console.log(x.Tuesday) //"More Work"
Can someone help explain why the array now behaves like an Object?
Share Improve this question asked Jan 18, 2018 at 7:39 user5844628user5844628 4191 gold badge6 silver badges17 bronze badges 4-
2
You've created an associative array where you have keys and values.
x.Monday
is not an entry with the value Monday, you're setting the keyMonday
to have the value ofWork
. Also, arrays extend Object, in-fact most things in Javascript extend object so theres that too – Halfpint Commented Jan 18, 2018 at 7:41 - 1 You'd be amazed how far you could take the bracket notation versus the dot notation: developer.mozilla/nl/docs/Web/JavaScript/Reference/… – rolfv1 Commented Jan 18, 2018 at 7:42
- A = { b : "hi"} looks the same as A['b'] = "hi"; because they both have keys to access value the only difference is that of the array is associative and the system knows which datatype you are referring to, normally an object and an array are similar just with certain things distinguishing them – Ezekiel Commented Jan 18, 2018 at 7:44
- Possible duplicate of Are Javascript arrays primitives? Strings? Objects? – Mohammad Usman Commented Jan 18, 2018 at 8:00
5 Answers
Reset to default 8Because array IS an Object
[] instanceof Object
> true
[] instanceof Array
> true
Both give you true because Array extends normal Object functionality. Furthermore, the array indexes all its members by string values
const array = [];
array[0] = 1;
array['0'] // will give you 1
which is also unexpected by most of the people. So, the array behaves like an Object even if you use it as a normal array with indices
Everything in JS is an Object except the Primitive types (and even those e with Object wrappers)
That is why you can access properties and methods like array.length
array.indexOf()
etc just like you would with any other object. The indices are like special properties on the array object which are iterable
over all JavaScript array is also an object
if you check the type of x
typeof x
it will prints "object"
var x =[];
x.name = "X";
here we are creating a property "name" of x array
Because in javascript there is no real Array
type as a low level type. Array
class is just a kind of a class which extended from Object
class.
- Every class in javascript except primitives like
Number
,String
ect. are extended fromObject
class. - Every object (instance of class) can have properties and methods.
- Every instance of classes can have new properties and methods on the run-time as well as this properties could be changed modified or deleted.
So by doing this.
var ar = [] // I used array literal. Not totally same but similar of doing new Array();
arr['foo'] = 'bar';
You are just adding a new property to an object.
I think you’ve heard by now that Array is a type of Object, so I won’t beat that dead horse any further. Well maybe a little.
Which means you can accsss and set properties with names like “Monday” and “Tuesday”. Since it’s a type of Object this is no problem (and a statement like a[‘1’]
is implicitly converting that string ‘1’ to a number 1). But don’t mistake this property name to be an array index, those are specifically integers**. What’s the importance of that?
Arrays have a nice behaviour that when you use an actual array index, the length
property is automatically updated for you. Not to mention array index access is usually optimized by JVM implementations to be noticeably faster than regular property accesses.
Also let’s not forget one of the other huge benefits of having Arrays, they inherit from the Array.prototype, which has a whole lot of useful methods :)!
So in short use Arrays like Arrays — gain the perf boost while also making sure the code is easier to prehend.
Source: JS Definitive Guide 6th - Array chapter
(** non-negative and less than 2^32-2 since arrays has 32-bit size indexes.)
I am trying to understand something in JavaScript.
Lets say I do the following:
let x = [];
x['Monday'] = 'Work';
x['Tuesday'] = 'More Work';
console.log(x) //[Monday: "Work", Tuesday: "More Work"]
console.log(x.Monday) //"Work"
console.log(x.Tuesday) //"More Work"
Can someone help explain why the array now behaves like an Object?
I am trying to understand something in JavaScript.
Lets say I do the following:
let x = [];
x['Monday'] = 'Work';
x['Tuesday'] = 'More Work';
console.log(x) //[Monday: "Work", Tuesday: "More Work"]
console.log(x.Monday) //"Work"
console.log(x.Tuesday) //"More Work"
Can someone help explain why the array now behaves like an Object?
Share Improve this question asked Jan 18, 2018 at 7:39 user5844628user5844628 4191 gold badge6 silver badges17 bronze badges 4-
2
You've created an associative array where you have keys and values.
x.Monday
is not an entry with the value Monday, you're setting the keyMonday
to have the value ofWork
. Also, arrays extend Object, in-fact most things in Javascript extend object so theres that too – Halfpint Commented Jan 18, 2018 at 7:41 - 1 You'd be amazed how far you could take the bracket notation versus the dot notation: developer.mozilla/nl/docs/Web/JavaScript/Reference/… – rolfv1 Commented Jan 18, 2018 at 7:42
- A = { b : "hi"} looks the same as A['b'] = "hi"; because they both have keys to access value the only difference is that of the array is associative and the system knows which datatype you are referring to, normally an object and an array are similar just with certain things distinguishing them – Ezekiel Commented Jan 18, 2018 at 7:44
- Possible duplicate of Are Javascript arrays primitives? Strings? Objects? – Mohammad Usman Commented Jan 18, 2018 at 8:00
5 Answers
Reset to default 8Because array IS an Object
[] instanceof Object
> true
[] instanceof Array
> true
Both give you true because Array extends normal Object functionality. Furthermore, the array indexes all its members by string values
const array = [];
array[0] = 1;
array['0'] // will give you 1
which is also unexpected by most of the people. So, the array behaves like an Object even if you use it as a normal array with indices
Everything in JS is an Object except the Primitive types (and even those e with Object wrappers)
That is why you can access properties and methods like array.length
array.indexOf()
etc just like you would with any other object. The indices are like special properties on the array object which are iterable
over all JavaScript array is also an object
if you check the type of x
typeof x
it will prints "object"
var x =[];
x.name = "X";
here we are creating a property "name" of x array
Because in javascript there is no real Array
type as a low level type. Array
class is just a kind of a class which extended from Object
class.
- Every class in javascript except primitives like
Number
,String
ect. are extended fromObject
class. - Every object (instance of class) can have properties and methods.
- Every instance of classes can have new properties and methods on the run-time as well as this properties could be changed modified or deleted.
So by doing this.
var ar = [] // I used array literal. Not totally same but similar of doing new Array();
arr['foo'] = 'bar';
You are just adding a new property to an object.
I think you’ve heard by now that Array is a type of Object, so I won’t beat that dead horse any further. Well maybe a little.
Which means you can accsss and set properties with names like “Monday” and “Tuesday”. Since it’s a type of Object this is no problem (and a statement like a[‘1’]
is implicitly converting that string ‘1’ to a number 1). But don’t mistake this property name to be an array index, those are specifically integers**. What’s the importance of that?
Arrays have a nice behaviour that when you use an actual array index, the length
property is automatically updated for you. Not to mention array index access is usually optimized by JVM implementations to be noticeably faster than regular property accesses.
Also let’s not forget one of the other huge benefits of having Arrays, they inherit from the Array.prototype, which has a whole lot of useful methods :)!
So in short use Arrays like Arrays — gain the perf boost while also making sure the code is easier to prehend.
Source: JS Definitive Guide 6th - Array chapter
(** non-negative and less than 2^32-2 since arrays has 32-bit size indexes.)
本文标签: JavaScript Array behaving like an objectStack Overflow
版权声明:本文标题:JavaScript Array behaving like an object - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745574134a2156913.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论