|Written by Ian Elliot|
|Thursday, 12 June 2014|
Page 1 of 4
There is a newer version of the draft of the book here.
Expressions form a mini-computer language inside most computer languages. The good news is that the way that they work is more or less the same in all languages. The symbols used for the operators change, the priorities change, and occasionally there are some additional operators.
An expression is essentially a generalization of the way arithmetic works. For standard arithmetic we only have four operators +, -, * and / standing in for the usual symbols for add, subtract, multiply and divide. These are all binary operators and take a value on the left and one on the right, the operands, and combine the two values to give a new value.
In many cases this distinction isn't important, but thinking in this way does make it easier to understand what is going on.
For example, take a careful look at:
What do you expect to see?
First we create a Number object with value 2 and a custom property myProperty. When you display the property it shows "Hello World" as you would expect. Next we add one to a and display its value, which is 3 as you would expect, but now the property is "undefined".
All we did was add one to a, surely this can't have modified the property?
It not only modified the property, it created a new object. When you add two Number objects together the result is a new Number object.
You can make this clearer by writing:
and you will see that now the + operator takes the object referenced by a and the Number object we have creates and adds their values together. As the result is a new object, it isn't surprising that it doesn't have the custom property you set on the first Number object.
Once you realize that this is the way things work it makes things seem more logical.
Of course primitive values are operated on in their unwrapped non-object state in reality but for the sake of a good logical story we can ignore this as it makes no practical difference to how things behave.
All objects have value
The only String operator is concatenation which is represented by the same symbol as addition, +.
The main Number operators are +,-,* and /. Other Number operators work in similar ways so it is reasonable just to consider these four.
You can use any object in an expression, not just Number and String. For example:
is perfectly valid irrespective of what the objects are.
How can general objects be included in expressions?
The valueOf method is intended to return an object that is somehow regarded as the "value" of the object.
Usually the value is a Number or a String but in fact it can be any object you care to return.
In practice, however, if it is going to be used with the standard operators it has to be a primitive object - i.e. a Number or a String.
When you use an object in an expression its valueOf method is automatically called to find a primitive value to use in the expression.
You can see that the myObject's valueOf function returns 1. You can also see that in:
myObject behaves as if it had a value of 1 - as you would expect.
in other words:
is equivalent to:
Notice that when you write
you can think of this too as:
All operands in an string or numeric expression have an implied valueOf call to provide the primitive value used in the evaluation of the expression.
The concatenation operator
This simple picture is made more confusing by the way + is used to represent addition and String concatenation.
The rule is very simple.
If either operand in
returns a String from its valueOf method then the operator is String concatenation.
In this case both valueOf methods return String objects and so the operator is concatenation and the result is
The same rule applies to the += operator which is concatenation if either a or b is a String in a+=b. However, notice that a+=b is the same as a=a+(b) which is not always the same as a=a+b because b will be evaluated first.
Now we come to the interesting question of what happens if only one of the two objects involved in the expression has a String value?
In this case by the rule we have just introduced the + is String concatenation but only one of the objects is a String.
The answer to this question is slightly more complicated than you might think - but very reasonable once you understand it.
But first a simple quiz.
To make sure you have all of this worked out, see if you can answer the following questions:
Try to work them out without typing in the code and then see if you are right by running the code. The answers and explanations are on the next page.
|Last Updated ( Sunday, 10 May 2015 )|