- Variables are not strongly-typed
- No native support for namespaces and classes (until ES6)
- Only 2 levels of variable scope (until ES6)
- Equality comparisons can be tricky
- Functions are objects by default
One of the biggest kind of “Sharks in the water,” is the Equality Comparisons. We’ll show some demos of the features later.
|Primary Types||Composite Types||Special Types|
What we have is Primary, Composite and some Special Types.
The Primary Types are pretty familiar with as C# developers. We have strings. We do have numbers. But if you’ll notice, numbers are not really broken down into small, double, float, long, short and that type of thing. It is just the number. Primary types can have decimals, they can be whole numbers, that really just depends.
In Special Types we also have null, which everybody is used too in the C#. But we have this other one, it is called Undefined which we’ll look at later in this post.
Instead of just checking that something is Null, you’ll often check if it is null or is it Undefined. I will show you some short cut ways that we can actually do that.
First half, we start with Equality.
One other difference to point out is, if you’ll notice I used the single quotes ‘ ‘ like a character in C#.
I mentioned undefined or null. You’ll notice address is not a signed value. If we actually to run this code right and we try to log the value of address, all it does is update the little bit of div at the top.
When I run this. It will show the output but the address is undefined, it doesn’t have a value.
It’s not null, it’s undefined.
That’s also different compared to C# because we don’t have an undefined in that rule.
Street however is null, it is a null pointer in memory. I’ll run this little page.
It’s not really impressive output, but you’ll see it’s true that quoted ‘5’ == 5
If we use the double quotes.
Here’s the trick I want to show you. It’s false if we use the triple equals ( = = = ).
I don’t like languages that coheres my type to make them equal to each other. You want to stick with the triple equals in almost any app you do. It is little bit hard to remember that, if you are coming from C# world, but that’s a big deal.
Also, you’ll notice that address is not null, it’s actually returning that it’s undefined.
That’s because it hasn’t been assigned a value yet, vs street, you can see is null that will be used to in C#. That would be one example of some of the differences.
If we do an Alert, the alert is like a message box that pops up. It is going to show it is 55. But notice in this loop, I am defining a new “age” and using that “age” to actually loop through and then riding out age.
Well, that’s going to be function scope, so we have global scope and function scope here.
Then down here at the bottom, you’ll notice I’m doing a block.
Just four loops standing on its own. I am also redefining “age” as 0.
Now the question is what’s that going to affect? Is it going to affect the top Global Scope age? Or is it going to affect the Function Scope?
Let’s run it and let’s see.
We should get a three or so message boxes. You will see the first time it ran, global age was 55.
That’s what we would expect that would be correct. We’ll click “ok” and then, the internal one in the function, when it gets done looping, it was 5.
I would expect that. But notice the last one, it should have written out 55. Instead, it wrote 5.
Now, you’re going to see that it’s going to change in ECMAScript 6. There’s a new keyword they are going to introduce called, “Let,” and that will make the age scope only to the block.
Now I can move up that person, just like its regular class.
That’s definitely unique compared to the C# type of approach because we are used to classes and you move up to the class.
Now, let’s say that I have a child that needs to inherit the functionality from base and what we really want to do is, when the child is run, we call log. What will happen is, it will first look at the child and ask, “Do you have a log function?” If you do, it will call him. If you don’t, it will then walk up the hierarchy which we are going to create in the next line of code and say “Do you have a log function?”
What we are going to do is to a have a child and we are going to assign to the child’s prototype, the new base.
Really, that’s going to make the prototype of the base object available here to our child.
After that, we are going to attach our own little add method. What is interesting here is, down in this code, when I call c.log after I move up the child, that’s going to look first in the child’s prototype and the local stuff and say “Do you have a log?”
It’s not going to find it. What it is going to do is, it’s going to walk up the prototype chain until it finds a prototype in a parent that has the log and then it would find this guy and then, it would run the function.
Unlike the “add” that you see in this example above, that actually would come in and just call the child’s add that’s on the child’s prototype itself.
Inheritance is totally different. This is one of the current ways that you can enable prototypal inheritance. ECMAScript 6 will be changing everything.
That’s a quick example of the few of the key differences and certainly there will be more I can cover, but that will get you thinking about how C# matches up.