|Written by Ian Elliot|
|Friday, 11 November 2016|
Page 2 of 4
The Global Object - Variables?
Note: if you know about "this" as used in functions then it is worth adding that it always refers to the Global object when used outside of a function. Later we will discover that "this" is always the current execution context and outside of a function it is the Global object which is the top level context.
The role of the Global object is to provide what in other languages would be referred to as global variables. You can create properties on the Global object dynamically simply by assigning to them.
creates a new Global object property called myObject and sets it to the empty object.
Notice that this is a property on the Global object and not what you would normally refer to as a global variable.
If you simply refer to a property without specifying the object, for example:
then it is assumed that it is the Global object you are referencing.
To make this look more like a variable declaration you can also write:
but these are just shorthand for:
As before remember that within a function "this" has a different meaning.
There is one subtle difference between:
The rule is that every:
statement is first split into:
and then the var part of the instruction is moved to the very top of the program.
In other words, the declaration is moved to the top but the initialization is left in place. This means that the property exists from the moment the program runs but it isn't initialized until execution reaches the place in the program you intended it to be initialized.
In most cases you can forget hoisting and just write:
where the initialization makes sense.
It is important to realise that variables, i.e. properties of the Global object, store references to objects. This might seem to be a small matter but it changes the way assignment works.
If you have a reference to an object and store it in another variable, for example:
then myObject2 references the same object as myObject1.
That is any changes you make to myObject1 will be made to myObject2 - they reference or point to the same object.
This is quite different to what happens in languages where variables store values. If a variable stores a value then assignment makes a copy of the value.
Built In Objects - String
Now we have the use of the Global Object's properties we can create object that we can retain a reference to and use and modify later. For example:
The problem is that because the only type of object we know about is the empty object this makes creating anything interesting difficult.
How all this fits together will become clearer as we proceed but for the moment all you need to know is that there is a String object which can represent a string of character.
You can create a new string object initialized to a given set of characters using;
is a string object that represents ABCD i.e. its value is ABCD.
There is a subtle point here. Notice that new String("ABCD") is a different object to new String("EFG"). A new object is manufactured each time we use new String.
Notice that if you know how things are actually implemented you will know that string are not implemented as full objects for reasons of efficiency. However how you should think about the high level aspects of a language is not the same thing as the messy details of its implementation.
Strings are objects and they have methods and properties.
Using this we can now create an example of a custom object with some properties:
This is an object with two properties Name and Address. You can guess that the idea is to use this object to represent a name and address. You can also see that we are using two String objects to as the objects associated with each property.
You can retrieve the properties in the usual way.
both specify the String object - String("John").
The next problem we have is that we have no way of verifying that any of this actually works - currently you have to take it on trust. We can solve this problem with alert.
The idea of the value of an object is something we will have to return to later because it is a more sophisticated an idea than you might think and it is very useful.
So for example:
displays the text
which isn't particularly useful but simply means that the object in question is a basic Object and not some other special built in object. If you try the same thing with a String object then you will see the text that it represents. For example
displays the text ABCD.
There are two simplifications we can make to the code. The first is that as window is the global object we can write:
The second is that the String object is so important that you can just write "ABCD" to create a String object with the value "ABCD".
So our display instruction can be reduced to:
Which is probably the form you already know how to use.
|Last Updated ( Friday, 11 November 2016 )|