Saturday, March 12, 2016

Things I've Learned

I Learned some interesting things about JavaScript.
It started with an assignment, and I just had a moment to hack out what it was I was discovering during spring break last week.
First thing:
When using JavaScript, you have to be mindful of your objects. It's very easy to change them, because when you pass an object to a variable, you pass the reference, and not just the values. In other words, the variable is the object, and not a copy of it. I captured this behavior in the screenshot below.


Screen shot of JavaScript and output























Another thing I learned:
Double quotes can bite you. When using a button to perform an action, I was having difficulty passing a parameter to a function. The reason why is because I had to pass the parameter using double quotes (it was a string id of a page element). I discovered that when you create a button, it's best to pass the action of the button in double-quotes. I then had to pass the parameter in single-quotes The code looked something like this:


getComp is a function I wrote to display the attributes of objects, hal2000 is the object whose attributes I will display, and conComp is the id of a p tag in the HTML of the page. I use it to display the object's attributes.
If I left out the 'conComp' part, and modified the function so it wasn't necessary (I hard coded a particular ID in place), then worked just fine. I even tried to single quote the second parameter, but that didn't work. The only way the javascript would run right was to Double quote the whole action parameter, and single quote the string parameter passed to the getComp function.

I'm more than halfway done with the school year and I'm learning a lot!
Post a Comment