-
-
Save 1n3ffbl3/88abdcfba1ec7148a0a927844ad06365 to your computer and use it in GitHub Desktop.
brackets and dots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// an example to learn about dots and brackets | |
// below are 6 test cases that work | |
// try writing some that break, find out what's happening | |
var og = {first: 'fifty', last: 'cent'}; | |
var curtis = 'first'; | |
var jackson = 'last'; | |
console.log(og.first); | |
console.log(og.last); | |
console.log(og[curtis]); | |
console.log(og[jackson]); | |
console.log(og["first"]); | |
console.log(og["last"]); | |
// -------------------------------- | |
var person = { | |
name: 'John', | |
lastName: 'Smith', | |
age: 32, | |
key: 0 | |
}; | |
for (let key in person){ | |
console.log(person[key]); | |
console.log(key) | |
console.log(person.key) | |
console.log("--------") | |
} | |
for (let mike in person){ | |
console.log(person[mike]); | |
console.log(mike) | |
console.log(person.mike) | |
console.log("--------") | |
} | |
// ----- objects vs arrays ----- | |
// run in python tutor | |
function toObject(arr) { | |
var rv = {}; | |
for (var i = 0; i < arr.length; ++i) | |
if (arr[i] !== undefined) rv[i] = arr[i]; | |
delete rv[1] | |
arr.splice(1, 1) | |
return rv; | |
} | |
toObject(["a", "b", "c"]) | |
// --- | |
// don't think about functions in objects just yet | |
// only string, numbers, ... | |
// --- | |
let obj = { | |
p: "roperty", | |
m: function ethod() { | |
console.log(this.p) | |
} | |
} | |
console.log(obj.p) // "roperty" | |
obj.m() // print "roperty" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment