|Written by Ian Elliot
|Friday, 11 November 2016
Page 3 of 4
Built-In Objects - Number
As you would guess just as there is a String object there is a Number object.
If you try:
you will see the number 3 displayed. You have created a Number object with the value 3.
This has implication that we have to look at later but for now you can just accept the simplification of not having to worry about different representations of numeric values. Don't underestimate the value of this simplification to a beginner. When you start to explain to a non-programmer that there are two types of number floats and ints you can see their eyes glaze over and a deep mistrust of such sillyness creeps into their soul. Later is the time to worry about integer v floating point operations not right at the start of learning and not in everyday programming. The language should take care of the messy things in life and if you don't agree with this there is always assembler waiting for your return.
You can dynamically create properties on Number objects as you can on any object and this can be something of a shock if you haven't encountered it before.
will display "My Age".
Of course, you don't have to write new Number() to create a Number object you can simply write its value, as in:
Now here we hit a snag - primitives.
The Primitive Problem
The same is true of String literals like "ABC" and of boolean, null and undefined - all of which will be described later. What matters at the moment is that idea that there are types of data that are not stored as objects for efficiency reasons.
actually creates a primitive value and not an object.
If you try to make use of a property of the Number object like
If you want to make use of custom properties on numbers you have to explicity box the value using the Number constructor as in the examples given earlier.
In practice this doen't have any real impact on what you want to write because you don't often want or need to add custom properties to the built in objects.
To be clear:
These exceptions are a shame because they spoil the deep principles.
Expressions and Immutability
One of the main tools of any programming language is the expression.
An expression takes data and combines it to produces new data.
For example, an arithmetic expression takes numbers and combines them to make new numbers. The ways in which the data can be combined takes the form of different operators. So arithmetic expressions have the addition operator and multiplication and so on.
is an expression that combines 2, 3 and 4 and produces a new value of 14.
combines three Number objects with values 2, 3 and 4 and creates a new Number object with the value 14.
In this sense you can imagine that the String and the Numeric objects are immutable - they don't change their values they are simply used within expressions to create new objects with different values.
For example if you try:
You will find that myObject2 has a myProperty and its value is "My Age". This is because myObject2 refers to the same Number object myObject.
However if you make the small change to the code an add one to myObject as in:
You will find that myObject2 has a value of 4 but it doesn't have a myProperty i.e. it is undefined.
The reason is that the arithmetic expression generates a new Number object that doesn't have a myProperty unless you go to the trouble of recreating it.
That is expressions that combine objects always create a new object.
Of course there is the complication of primitive data but in the main you can ignore it. The simple reason is that a primitive data value behaves as if it has only the default properties of the object and so when you combine them in an expression you get something that has just the default properties. Once again it looks as if expression take in objects and spit out a new object - even with primitive values.
In short you can alway think of expressions as working with objects and creating a new object as the result.
|Last Updated ( Friday, 11 November 2016 )