I don’t know whether to post here or to create my own topic, but i will just post here since i am on this part of the lecture.
Firstly i created 2 functions like so:
<script>
function greet (name, number)
{
document.write("<h1> Hey there " + name + "your number is " + number + "</h1>");
}
greet("Ted", 12);
function greetTwo (name)
{
document.write("<h1><i> Hey there " + name + "</i></h1>");
}
greetTwo("Victoria");
</script>
The result is simple:
Hey there Tedyour number is 12
Hey there Victoria
Now i tried experimenting and named the renamed the ‘greetTwo’ function to ‘greet’. Basically both functions are now with the same name ‘greet’.
<script>
function greet (name, number)
{
document.write("<h1> Hey there " + name + "your number is " + number + "</h1>");
}
greet("Ted", 12);
function greet (name)
{
document.write("<h1><i> Hey there " + name + "</i></h1>");
}
greet("Victoria");
</script>
What i was trying to do is see how the code would execute. In theory i thought it would firstly execute the first ‘greet’ [greet(“Ted”, 12);] and i would get 'Hey there Ted your number is 12’
Then the second ‘greet’ would be overwritten, leaving the ‘Hey there Ted your number is 12’ as it is. Then using the [greet(“Victoria”);] command i would call out the newly created ‘greet’ function without a number. ‘Hey there Victoria’. But the result i got is weird in my opinion.
Hey there Ted
Hey there Victoria
This is what i got.
What is weird is this is how i imagined the code going:
function greet (name, number)
greet("Ted", 12);
function greet (name)
greet("Victoria");
What i got in theory is:
function greet (name)
greet(“Ted”, 12);
greet(“Victoria”);
So from what i understand, the code already knows the functions in the whole code. So when i call those functions, the last overwritten function (from the whole code) would be executed. So if i was to overwrite ‘greet’ a million times, the program would still use the last ‘greet’ that was overwritten for the whole code.
The example of my last statement:
function greet (name) {document.write("HEY THERE " + name); }
greet("Ted");
function greet (name) {document.write("<p> HOW YOU BEEN " + name); }
greet("Lacy");
greet("Lacie");
greet("Lacyeyeye");
function greet (name) {document.write("WASSUP@@@@ " + name); }
greet("Onut");
function greet (name) {document.write("EYO MA FRIEND" + name); }
function greet (name) {document.write("<p> hey " + name); }
greet("Micheal");
The result of this:
hey Ted
hey Lacy
hey Onut
hey Micheal
Only the last overwritten ‘greet’ was done. But it executed all previous calls of ‘greet’ in the code (like Lacy, Lacie, Onut).
I guess this is what you call a global function, but knowing that it would work like this is very interesting.