Meaning of this: functions in the global scope

for CSCI E3, Harvard University author(s): Larry Bouthillier

by DustyWhite

HTML

<h3>Open your console to see the output</h3>
<p>You can see here that <i>this</i> gets the value of the context from which the function was called.  Remember that variables declared in the global context are actually properties of the Window object.</p>
<p> So <code>sayIt()</code> is actually <code>window.sayIt()</code>  </p>
<p> and so, inside sayIt(), the value of <i>this</i> is the window object. </p>

JavaScript

/* function sayIt(){
    console.log("this is :" + this);   
    console.log(this);   
}
sayIt(); */


function AddrBookEntry(f, l, a, e) {
    this.fname = f;
    this.lname = l;
    this.addr = a;
    this.email = e;

    this.personRole = "student";
    this.getFullName = function(){
                return this.fname + " " + this.lname;
    }
    
    console.log("this is :" + this.fname  + " " + this.lname);   
    console.log(this.fname);  
}

new AddrBookEntry("Sarah", "Connor", "Los Angeles", "[email protected]");

AddrBookEntry();