Skip to content

Instantly share code, notes, and snippets.

@1n3ffbl3
Forked from colevandersWands/braks-n-dots.js
Created April 12, 2018 10:45
Show Gist options
  • Save 1n3ffbl3/88abdcfba1ec7148a0a927844ad06365 to your computer and use it in GitHub Desktop.
Save 1n3ffbl3/88abdcfba1ec7148a0a927844ad06365 to your computer and use it in GitHub Desktop.
brackets and dots
// 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