Javascript dates, scoping and change events

Javascript dates, scoping and change events

I have previously written about Javascript issues that may arise while you are developing a javascript app. For example if you have a server and it is serving Javascript and html. Chances are you are needing to send down information to the view. So I have discussed the various ways you can do that below. I also have a few blog articles that go over some small code snippets and useful tips.

Passing variables from the server to Browser
Recently I have been struggling with trying to figure out a good practice forsending information to the view from the server. The main issue I had with thiswas that I couldn’t seem to figure out a good practice for sending someinformation down without it being exposed on the DOM in some fashion. …
Code Snippets of Html, Css, Javascript
As I work throughout the day I will run into challenges that I have to workouton my own. A lot of the time have to work them out manually to understand whatthe problem is. Most of the time they are small tidbits but they are quiteuseful for the work I am doing. I hope some of these are surprising…
Javascript Performance and Snippets part 2
I have a bit more time playing around with JavaScript and I decided that it istime to post another JavaScript tips and tricks/gotchas. In today’s blog I willbe going over some of efficiencies and draw backs of using libraries that I havebecome aware of. Also I will be posting some of the gotchas …

Property Change Events

Alright, down to business. Over recent frustrations with extremes, I have ventured to learn what Javascript will do without bringing in any libraries. One such frustration had to do with people at work abusing Angular's $Watch feature. After we discovered what kind of issues it presented we switched over to Angular's event broadcasting feature. After being frustrated in feeling like both features were getting overly used without much thought being put into them; I checked to see what Javascript had to offer right out of the box.

I created a property change event using the getters and setters from Javascript. I can't say it is the prettiest syntax nor the best way to go about doing things, but it goes to show you don't always need to use a library to get things done.

For this code example below, I created a counter object. This object has an internal count variable. onChange is for printing but it can be set to whatever you want Function wise but doesn't need to be set.

For the getter all I do is up the count by 1. For the setter all I do is print and set the value. Fairly simple example but proves the point and demonstrates how it could be done if you wanted to implement such a feature. I am not sure how supported it is in older browsers.

Scoping

Scoping is always fun to try to debug in Javascript. If you know anything about Javascript, then you probably have already discovered this. Instead of me ruining the surprise, can you understand why the example below is printing out what it does?

Scope is passed down from any parent function. This can be dangerous if you aren't careful. Mixing scopes can be powerful but troublesome if your variables are not named appropriately. this will not reference the parent unless you assign it to a variable in the parent and then not override it in the child. That is important to understand when programming in Javascript.

Dates

If you can use a library for dates, I would say do it. Dates are tricky objects in Javascript. Depending on how the date is passed into the date object, it can change the result by years. Below I have an example of the differences it can makes when you pass in a date via string or use the comma separated approach.

So there you have it. If you aren't paying close attention to what the language is doing, you might just have a person that is born in 1900's or 2000's. And if you don't have your scoping right, you might not even get their birthday set in the first place.