Page 1 of 3
Buy Now: from your local Amazon
An Idiomatic Approach
- In The Beginning Was The Object
- Real World Objects
- The Function Object
Extract - The Function Object
Extract - Function Object Self Reference
- The Object Expression
- Function Scope, Lifetime & Closure
Extract Scope, Lifetime & Closure
Extract Execution Context ***NEW!
- Parameters, Returns and Destructuring
Extract - Parameters, and Destructuring
- How Functions Become Methods
- Object Construction
Extract: - Object Factories
- The Prototype
Extract - ES2015 Class and Extends
- Inheritance and Type
- The Search For Type
- Property Checking
Buy Now: from your local Amazon
Also by Ian Elliot
Just jQuery: The Core UI
Just jQuery: Events, Async & AJAX
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:
var a=new Number(2);
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 created 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.
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
It turns out that the way Boolean operators work is different for various reasons.
For now let's just concentrate on Number and String operators.
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?
Methods are discussed in detail in Chapter 8 but for now you can treat a method as a property of an object that is used like a function.
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.
var a=new Number(2);
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 a 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:
Object1 + Object2
returns a String from its valueOf method then the operator is String concatenation.
a = myObject1 + myObject2;
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.