Javascript Gotchas & Git Usage
After a week of coding full time at DevLeague, we encountered common coding errors for javascript newbies. These were:
Sometimes we want the values of properties in an object (ie: obj). If we have the name of the property assigned to a variable named key, we can get the property’s value. When the property name is not a literal string but stored in a variable, you must use the bracket notation instead of the dot notation to get the value:
var obj = {word : ‘hello’};
for (var key in obj) {
console.log(obj[key]); // logs ‘hello’
console.log(obj.key); // logs undefined since there’s no property named key
}
Sometimes we assign a function to a variable, but forgot to invoke it. To get the value of the function, you must invoke the function via the parentheses operator ():
function someFunc () { return true; };
var anotherFunc = someFunc;
var value = anotherFunc; // === Function
var value = anotherFunc(); // === true;
Also, we worked with git a lot to clone remote repositories and create local ones. To review:
Create remote repositories either by forking an existing one or creating a new one on github.com (click ‘+’ at top right).
To work with repositories: If you forked an existing remote repository (ie: for projects), do steps B then C; if you created a repository (ie: for gists), do steps A then C.
A. git create local repositories:
mkdir <folder name> in the parent directory
cd <folder name> to change to working directory
git init (this creates .git folder)
git remote add origin <remote repository address>
B. git clone remote repositories:
“git clone <remote ssh address>” in the parent directory
cd <folder name> to go to working directory
C. git syncing files to remote repositories:
git add <filename> = stage a file to be committed
git commit -m “message” = finalizes changes LOCALLY
git push -u origin master = sync changes REMOTELY, -u sets default syncing: to origin from master (later push commands can just be “git push”)