Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript object as array

Accessing Js object properties :

What is the difference between :

1) Obj.src
     vs
2) Obj["src"]

I only know that #1 can't be used with number ( e.g. if the property name is "1")

Is there any other major differences ?

like image 631
Royi Namir Avatar asked Apr 15 '26 18:04

Royi Namir


1 Answers

No difference really. Old'ish browser showed some performance differences using either the one or the other notation, but those times are over.

The only important difference is that the bracket notation is your only chance to dynamically access Object properties.

For instance:

var access = 'src';

Obj[ access ];

you can't do that using a dot notation.

like image 192
jAndy Avatar answered Apr 17 '26 07:04

jAndy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!