@@ -833,9 +833,261 @@ prints:
833
833
[ 'x', 'y', 'abc' ]
834
834
```
835
835
---
836
+ # nested data structures
837
+
838
+ Now that we've seen objects and arrays, it's worth noting
839
+ that you can nest these data structures as much as you
840
+ like.
841
+
842
+ To reference the nested properties, you can stack dot and
843
+ square bracket notation to read keys and array indexes:
844
+
845
+ ``` js
846
+ var root = {
847
+ a: 3 ,
848
+ b: [ { x: 4 , y: 6 }, { d: 1 , e: 2 } ],
849
+ c: { q: [ 7 , 8 , 9 ], r: [ 1 , 2 , 3 ]
850
+ };
851
+ console .log (root .a ); // 3
852
+ console .log (root .b [1 ].e ); // 2
853
+ console .log (root .c .q ); // [ 7, 8, 9 ]
854
+ console .log (root .c .r [2 ]); // 3
855
+ ` ` `
856
+ ---
857
+ # while loop
858
+
859
+ Use a while loop to repeat a command while a conditional is
860
+ true:
861
+
862
+ ` ` ` js
863
+ var x = 4 ;
864
+ while (x > 0 ) {
865
+ console .log (x);
866
+ x -- ;
867
+ }
868
+ ` ` `
869
+
870
+ prints:
871
+
872
+ ` ` `
873
+ 4
874
+ 3
875
+ 2
876
+ 1
877
+ ` ` `
878
+ ---
879
+ # while loop: break
880
+
881
+ This program will never stop!
882
+
883
+ ` ` ` js
884
+ while (true ) {
885
+ console .log (' wow' );
886
+ }
887
+ ` ` `
888
+
889
+ But inside the loop we can use ` break ` to quit the loop:
890
+
891
+ ` ` `
892
+ var x = 4 ;
893
+ while (true ) {
894
+ console .log (x);
895
+ if (x <= 0 ) break ;
896
+ }
897
+ ` ` `
898
+
899
+ prints:
900
+
901
+ ` ` `
902
+ 4
903
+ 3
904
+ 2
905
+ 1
906
+ ` ` `
907
+ ---
908
+ # for loop
909
+
910
+ For loops are like while loops but provide a place for
911
+ initialization and an expression to advance the loop.
912
+
913
+ ` ` `
914
+ for (var i = 0 ; i < 4 ; i++ ) {
915
+ console .log (i);
916
+ }
917
+ ` ` `
918
+
919
+ prints:
920
+
921
+ ` ` `
922
+ 0
923
+ 1
924
+ 2
925
+ 3
926
+ ` ` `
927
+ ---
928
+ Or you can jump by tens starting from 50:
929
+
930
+ ` ` ` js
931
+ for (var i = 50 ; i < 100 ; i += 10 ) {
932
+ console .log (i);
933
+ }
934
+ ` ` `
935
+
936
+ prints:
937
+
938
+ ` ` `
939
+ 50
940
+ 60
941
+ 70
942
+ 80
943
+ 90
944
+ ` ` `
945
+ ---
946
+ # looping over an array
947
+
948
+ A very common use-case for ` for ` loops is to loop over each
949
+ element in an array:
950
+
951
+ ` ` ` js
952
+ var xs = [ ' a' , ' b' , ' c' , ' d' ];
953
+ for (var i = 0 ; i < xs .length ; i++ ) {
954
+ console .log (xs[i]);
955
+ }
956
+ ` ` `
957
+
958
+ prints:
959
+
960
+ ` ` `
961
+ a
962
+ b
963
+ c
964
+ d
965
+ ` ` `
966
+ ---
836
967
# functions
837
968
838
- You can think of functions as little factories.
969
+ You can think of functions as little factories that take
970
+ input as arguments and return output.
971
+
972
+ ` ` ` js
973
+ function add (x , y ) {
974
+ return x + y;
975
+ }
976
+ console .log (add (3 , 4 ));
977
+ ` ` `
978
+
979
+ prints:
980
+
981
+ ` ` `
982
+ 7
983
+ ` ` `
984
+ ---
985
+ # function hoisting
986
+
987
+ You can define a function lower down in a file than where
988
+ you use it too:
989
+
990
+ ` ` ` js
991
+ console .log (add (3 , 4 ));
992
+
993
+ function add (x , y ) {
994
+ return x + y;
995
+ }
996
+ ` ` `
997
+ ---
998
+ # function scope
999
+
1000
+ Any variables declared inside a function are only accessible
1001
+ inside of that function.
1002
+
1003
+ ` ` ` js
1004
+ function fff (a , b ) {
1005
+ var c = 500 ;
1006
+ return a + b + c;
1007
+ }
1008
+
1009
+ console .log (fff (3 ,4 ));
1010
+ console .log (c); // will raise an error
1011
+ ` ` `
1012
+ ---
1013
+ # functions as values
1014
+
1015
+ Functions can appear in any expression. You can assign them
1016
+ to variables:
1017
+
1018
+ ` ` ` js
1019
+ var add = function (a , b ) {
1020
+ return a + b;
1021
+ };
1022
+ console .log (add (5 , 2 )); // 7
1023
+ ` ` `
1024
+
1025
+ When you declare functions in expressions you don't need to
1026
+ give them a name.
1027
+ ---
1028
+ # immediately executing function
1029
+
1030
+ Because functions can appear in expressions, you can
1031
+ immediately execute a function by defining the function
1032
+ immediately followed by ` (... )` to call the function with
1033
+ arguments.
1034
+
1035
+ ` ` ` js
1036
+ var sum = function (a , b , c ) {
1037
+ return a + b + c;
1038
+ }(3 , 4 , 5 );
1039
+ console .log (sum); // 12
1040
+ ` ` `
1041
+
1042
+ Variables declared inside functions are only visible in that
1043
+ function, so immediately executing functions are sometimes
1044
+ used to create an isolated scope.
1045
+ ---
1046
+ You could even do:
1047
+
1048
+ ` ` ` js
1049
+ console .log (100 + function (a , b , c ) {
1050
+ return a + b + c;
1051
+ }(3 , 4 , 5 ) * 2 );
1052
+ ` ` `
1053
+
1054
+ which is the same as ` 100 + 12 * 2 ` , so the program prints:
1055
+
1056
+ ` ` `
1057
+ 124
1058
+ ` ` `
1059
+ ---
1060
+ # higher-order functions
1061
+
1062
+ Because functions are ordinary values that can appear in
1063
+ expressions, you can pass a function as an argument to
1064
+ another function.
1065
+
1066
+ ` ` ` js
1067
+ function binary (a , b , operator ) {
1068
+ return operator (a, b);
1069
+ }
1070
+ function add (a , b ) {
1071
+ return a + b;
1072
+ }
1073
+ console .log (binary (3 , 4 , add));
1074
+ ` ` `
1075
+
1076
+ ---
1077
+ # return values
1078
+
1079
+ Functions can return any javascript value: numbers, strings,
1080
+ arrays, objects, even other functions!
1081
+
1082
+ ---
1083
+ # inline functions
1084
+
1085
+ You can define a function
1086
+ ---
1087
+ # constructors
1088
+
1089
+ Constructors are functions for pumping out instances.
1090
+
839
1091
840
1092
---
841
1093
# builtins
@@ -973,19 +1225,10 @@ console.log([ d.getFullYear(), d.getMonth(), d.getDay() ])
973
1225
974
1226
console .log ([ d .getHours (), d .getMinutes (), d .getSeconds () ])
975
1227
// [ 10, 18, 26 ]
976
- -- -
977
- # while loop
978
-
979
- -- -
980
- # for loop
981
-
982
1228
-- -
983
1229
# the REPL
984
1230
985
1231
-- -
986
- # constructors
987
-
988
- Constructors are functions for pumping out instances.
989
1232
990
1233
-- -
991
1234
# homework
0 commit comments