|
1 |
| -var character = 'mario'; |
2 |
| -console.log(character); |
3 |
| -var inputs = document.querySelectorAll('input'); |
4 |
| -inputs.forEach(function (input) { |
5 |
| - console.log(input); |
6 |
| -}); |
| 1 | +var character = "mario"; |
| 2 | +//character inferred to always be a string |
| 3 | +var age = 30; |
| 4 | +//age always inferred to be a number |
| 5 | +var isBlackBelt = false; |
| 6 | +//isBlackBelt always inferred to be a boolean |
| 7 | +//character = 20 not allowed because it isn't type: string |
| 8 | +character = "luigi"; //is allowed because the reassignment is to the same type |
| 9 | +// type a parameter being passed into a function with trailing : followed by the type |
| 10 | +var circumference = function (diameter) { |
| 11 | + return diameter * Math.PI; |
| 12 | +}; |
| 13 | +//when invoked with an argument this will error out unless the correct type is passed in |
| 14 | +console.log(circumference(7.5)); |
| 15 | +//arrays |
| 16 | +var names = ["luigi", "mario", "yoshi"]; |
| 17 | +names.push("toad"); //works because it matches the types in the array when it was instantiated |
| 18 | +// names.push(3) --> errors out because 3 isn't type: string |
| 19 | +// names[0] = 3; --> also disallowed because we're trying to reassign an index that was type: string to a number |
| 20 | +//can do a mixed array |
| 21 | +var mixedArray = ["ken", 4, "chun-li", 8, 9, false, { name: "shane" }]; |
| 22 | +mixedArray.push("kirk"); |
| 23 | +mixedArray.push(3); |
| 24 | +mixedArray[0] = 99; |
| 25 | +//objects |
| 26 | +var ninja = { |
| 27 | + name: "mario", |
| 28 | + belt: "black", |
| 29 | + age: 30 |
| 30 | +}; |
| 31 | +ninja.age = 40; //this is fine, updating property with type: string with another string |
| 32 | +// ninja.name = 40 --> won't work, reassigning string to number once it was already defined |
| 33 | +//this is allowed because ninja has the same properties and same types on those porperties |
| 34 | +// can't change name to username, would error out and can't change the types of any of the property values. You have to use the initial structure of the object from when it was declared |
| 35 | +ninja = { |
| 36 | + name: "yoshi", |
| 37 | + belt: "yellow", |
| 38 | + age: 40 |
| 39 | +}; |
0 commit comments