Wednesday, May 16, 2012

Wednesday, February 1, 2012

What does it equal?

Equality operators are operators that compare two values ​​to determine whether they are identical or different and return a boolean (true or false) depending on the result of the comparison. For equality comparison we have two operators in javascript one is Equals(==) operator and the other is Strict Equals(===) operator.
Equals Operator
The Equals operator is quite liberal that is the values may be considered equal even if their types are not same. Javascript features weak typing. This means that the equality operator coerces types of one or both operands into a single type (usually a number) to perform comparison. Below is the list of some interesting conditions and their results.
Condition
Result
“” == “0″
false
0 == “”
true
0 == “0″
true
false == “false”
false
false == “0″
true
false == undefined
false
false == null
false
null == undefined
true
” \t\r\n” == 0
true

Now lets see how ECMA defined == logic. Only thing is that undefined and null equal each other (and nothing else) and most other types get coerced to a number to facilitate comparison:
Type(x)
Type(y)
Result
null
undefined
true
undefined
null
true
Number
String
x == toNumber(y)
String
Number
toNumber(x) == y
Boolean
any
toNumber(x) == y
any
Boolean
x == toNumber(y)
String or Number
Object
x == toPrimitive(y)
Object
Sring or Number
toPrimitive(x) == y
Both are of same type
x === y (srict equals)
Otherwise
false

Where the result is an expression the algorithm is reapplied until the result is a boolean. toNumber() and toPrimitive() are internal methods which convert their arguments according to the following rules:
toNumber():
Argument Type
Result
undefined
NaN
Null
+0
Boolean
Result is 1 if argument is true
Result is 0 if argument is false
String
Parses to number.
For ex:
‘abc’ returns NaN
’123′ returns 123
Object
first primValue = toPrimitive(argument)
then toNumber(primValue)
toPrimitive():
Argument Type
Result
Object
(in the case of equality operator coercion) if valueOf returns a primitive, return it. Otherwise if toString returns a primitive return it. Otherwise throw an error
otherwise
The result equals the input argument (no conversion).

Lets see a simple condition and how its value is deduced
Condition: “SomeString” == true
Step1: First boolean is converted into number using toNumber. So now the resultant condition would be
“SomeString” == 1
Step2: Next the String is converted to Number again using the toNumber. So the resultant condition would be
Nan == 1
Result: now the above conditio returns false
Strict Equals Operator (===)
The strict equality operator consists of three equal signs: ===. It works exactly like the normal equality operator, except that strict equality operator does not perform type coercion between its operands. Below is the list of some interesting conditions and their results.
Condition
Result
“” === “0″
false
0 === “”
false
0 === “0″
false
false === “false”
false
false === “0″
false
false === undefined
false
false === null
false
null === undefined
false
” \t\r\n” === 0
false
Lets see when to choose equals and StrictEquals operators
The strict equals operator is a lot clearer and allow early breakage of code(no coercion). This hardens code to a certain degree and also gives performance improvements in case the operands are of different types. But completely avoiding the equals operator is also not wise. For example consider the following conditions
Condition1: typeof variable1 === “function”
Condition2: typeof variable1 == “function”
Considering the above two conditions the condition2 is a better option. The reason being typeof returns a string, so the operation always compares two strings , hence it is completely coercion proof.
So when there is no chance for coercion then == operator can be used at its best performance. The decision should be taken wisely.

Tuesday, January 31, 2012

Wednesday, November 2, 2011

Javascript RPG Game tutorial part 2

Continued from Javascript RPG Game tutorial part 1

So, there is a Object type called 'Hero' and we made an instance called 'hero'. I sometimes call my Jetta 'Stupid Face'. My Jetta's name is 'Stupid Face'. Our hero's name is 'bill'. But what if you'd like to call your hero something like 'donut pants'? Easy enough!

Your overall code looks like (or should anyway):
<!DOCTYPE html>
<head>
<title>Dungon Game</title>
<script type="text/javascript">
var Hero = function(name,type){
this.my_name = 'bill';
this.my_type = type;
}

var hero = new Hero();

console.log(hero);
console.log(hero.my_name);
</script>
</head>
<body>

</body>
</html>

Lets change that to:
<!DOCTYPE html>
<head>
<title>Dungon Game</title>
<script type="text/javascript">
var Hero = function(name,type){
this.my_name = name;
this.my_type = type;
}

var hero = new Hero('donut pants');

console.log(hero);
console.log(hero.my_name);
</script>
</head>
<body>

</body>
</html>
Reload your web page and look at the firebug console!

You should see something like:

Object { my_name="donut pants"}
donut pants

Ok.. thats great! Now we can make as many 'instances' of heros that we want! so lets delete the part
that says:

var hero = new Hero('donut pants');

and replace it with:

var Thundar = new Hero('Thundar', 'the barbarian');
var Ariel = new Hero('Ariel', 'the sorceress');
var Ukla = new Hero('Ukla', 'the faithful companion');

Reload your page and tada! You have new entries in your firebug console!

You should see this:

Object { my_name="Thundarr", my_type="the barbarian"}
Object { my_name="Ariel", my_type="the sorceress"}
Object { my_name="Ookla", my_type="the mok"}

If you see this you're doing great. If not double check your code.

Ok, so what are we looking at here? We are looking at 3 instances of the Hero 'Object' or 'Class'.
They are all cut from the same plan, but they have different names and types.

Lets take a step back for a second though. There are plenty of things I haven't explained yet so
lets explain a couple.

Notice the 'var' before several lines of code? That is short for 'variable'. Basically what programming
essentially does is manipulate data and then display that data. In javascript our data are typically
'variables'. Variables can be different things. They can be numbers:

var hot_girls_in_class = 2;

They can be a 'String' of text:

var name_for_my_pos_car = 'Stupid Face';

They can be a list of things, also called an Array:

var todo_list = new Array('clean room', 'actually clean room', 'feed cat', 'watch tv');

... and as we've already seen it can be an 'Object'.

I started with objects because they are easy to understand if you don't over think them, and they
are harder to understand than all the other types. What might be confusing going forward is that
variables can also be functions.

But what is a function? Lets go back to the car metaphor.

All Jettas have some characteristics and operations.
For example Jettas have:
doors,
windows,
wheels,
sunroof,
bike rack,
windsheild,
gastank,
engine,
transmission,
etc...

those are characteristics that can vary from model to model.

Jettas can:
Accellerate,
Slow down,
Turn,
Turn On,
Turn Off,
Change gears,
Attract hot girls (ok maybe not)

What an object 'has' are properties. What an object "can do" is a 'function'. Later on you'll call them methods, but for now lets stick
with functions.