Java Reference
In-Depth Information
fName = undefined, lName = undefined
fName = John, lName = Jacobs, full name = John Jacobs
john = undefined
ken = undefined
fName = Ken, lName = McEwen, full name = Ken McEwen
You can observe the following points in the code:
this.fName and this.lName . The
printf() function is called in the global context and the keyword
this refers to the global object. Because you have not defined any
fName and lName properties for the global object, values for both
properties are returned as undefined .
The code prints the value of
Person() function is called and its return value is stored in
the john variable. The Person() function is called in the global
context, so the keyword this inside the function refers to the
global object. The function adds three properties to the global
object.
The
printf() function is used to print the details of the global
object (referred to by this ) to confirm that the previous call to the
Person() function added those properties to the global object.
The
Person() function is called again with different arguments.
The first call had already added three properties to the global
object. This call simply updates them with new values. This is
confirmed by reading these properties from the global object
again.
The
Person() function does not return a value explicitly,
it returns undefined by default that is confirmed by the last two
lines in the output.
Because the
If you do not pass any arguments a constructor, you can omit the parentheses in the
new object creation expression. The following code calls Person as a constructor function
without passing any arguments that will default the actual parameters inside the function
to undefined . Notice that the new operator is simply followed with the constructor name:
// Create a person with both names as undefined
var person = new Person; // No arguments list
print(person.fullName());
// Set the names for the person
person.fName = "Ken";
person.lName = "Smith";
print(person.fullName());
 
Search WWH ::




Custom Search