Skip to content

Commit 474365c

Browse files
author
James Halliday
committed
objects
1 parent bb2913c commit 474365c

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed

javascript.markdown

+196
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,25 @@ second: 4
458458
third: 9
459459
```
460460
---
461+
# arrays: indexing with variables
462+
463+
You can use variables for indexing too, not just constant
464+
integers:
465+
466+
``` js
467+
var arr = [ 1, 4, 9 ];
468+
var n = 2;
469+
console.log(arr[n]);
470+
```
471+
472+
prints:
473+
474+
```
475+
9
476+
```
477+
478+
Any expression inside the square brackets will work.
479+
---
461480
# arrays: length
462481

463482
To get the length of an array, just use `.length`:
@@ -621,6 +640,183 @@ We've already been using some objects:
621640
* `console` is an object with a property
622641
`log` that is a function.
623642
---
643+
# objects
644+
645+
Objects map keys to values.
646+
647+
Like how a phone book maps names to telephone numbers:
648+
649+
```
650+
key value
651+
----------------------|---------
652+
Benjamin Franklin | 123-4142
653+
Alexander Graham Bell | 214-6821
654+
Marie Curie | 615-2904
655+
```
656+
---
657+
# objects
658+
659+
To create the same structure in javascript we could do:
660+
661+
``` js
662+
var phonebook = {
663+
'Benjamin Franklin': '123-4142',
664+
'Alexander Graham Bell': '214-6821',
665+
'Marie Curie': '615-2904'
666+
};
667+
```
668+
---
669+
# objects
670+
671+
If a key is only letters, numbers, underscores, and dollar
672+
signs (but doesn't start with a number), you can leave off
673+
the quotes:
674+
675+
``` js
676+
var obj = {
677+
abc: 555,
678+
def: 123,
679+
x1: 1000,
680+
y1: 5000,
681+
$x: 4444,
682+
"x y z": 12 // otherwise you'll need to use quotes
683+
}
684+
```
685+
686+
This way of building objects is sometimes called
687+
"object literal" syntax.
688+
---
689+
# objects: pick out a single record
690+
691+
Once an object exists, you can reference an individual value
692+
by its key using a `.` followed by the key name. Here we use
693+
`obj.y` to get at the `y` key:
694+
695+
``` js
696+
var obj = { x: 7, y: 8, z: 9 };
697+
console.log(obj.y);
698+
```
699+
700+
will print:
701+
702+
```
703+
8
704+
```
705+
---
706+
# objects: create a new record
707+
708+
Reference a key with a `.` followed by a key then use an
709+
equal sign to create a new value under that key:
710+
711+
``` js
712+
var obj = { a: 3, b: 4 };
713+
obj.c = 5;
714+
console.log(obj);
715+
```
716+
717+
prints:
718+
719+
```
720+
{ a: 3, b: 4, c: 5 }
721+
```
722+
---
723+
# objects: update an existing record
724+
725+
The same `obj.c = 5` syntax works even if a key already
726+
exists:
727+
728+
``` js
729+
var obj = { x: 500, y: 200 };
730+
obj.y = 300;
731+
console.log(obj);
732+
```
733+
734+
prints:
735+
736+
```
737+
{ x: 500, y: 300 }
738+
```
739+
---
740+
# objects: assignment operators
741+
742+
All of the assignment operators work for object keys just
743+
like for variables!
744+
745+
``` js
746+
var obj = { x: 500, y: 200 };
747+
obj.y += 1000;
748+
obj.x *= 3;
749+
console.log(obj);
750+
```
751+
752+
prints:
753+
754+
```
755+
{ x: 1500, y: 1200 }
756+
```
757+
---
758+
# objects: square brackets
759+
760+
Using the `.` operator to access keys is handy, but what
761+
about special characters in keys?
762+
763+
We can use square brackets with a string to reference
764+
keys with special characters:
765+
766+
```
767+
var obj = { 'a b c': 123, 'x#y*z': 456 };
768+
console.log(obj['a b c']);
769+
```
770+
771+
prints:
772+
773+
```
774+
123
775+
```
776+
---
777+
# objects: square brackets
778+
779+
We can also use square brackets to reference dynamic keys.
780+
For example, if we want to load a key from a variable:
781+
782+
``` js
783+
var key = 'def';
784+
var obj = {
785+
abc: 555,
786+
def: 333,
787+
xyz: 222
788+
};
789+
console.log(obj[key]);
790+
```
791+
792+
prints:
793+
794+
```
795+
333
796+
```
797+
798+
Everything we can do with dot (updates, assignment
799+
operators, etc) works with square bracket notation.
800+
---
801+
# objects: delete
802+
803+
To delete items from an object, use the `delete` keyword:
804+
805+
``` js
806+
var obj = { a: 4, b: 5, d: 700, x: 6 };
807+
delete obj.d;
808+
console.log(obj);
809+
```
810+
811+
prints:
812+
813+
```
814+
{ a: 4, b: 5, x: 6 }
815+
```
816+
817+
If you try to delete a key that doesn't exist, nothing
818+
happens.
819+
---
624820
# Object.keys
625821

626822
Use `Object.keys()` to get an array of all the keys that an

0 commit comments

Comments
 (0)