/ Javascript

Javascript dates, scoping and change events Part 3

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 here: Variable passing for Javascript from server to client. I also have a few blog articles that go over some small code snippets and useful tips. Checkout Code Snippets Part 1 and Code Snippets Part 2.

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.