28. Passing Objects into Functions


Code is fine but I can't work out why "person1" + "person2" (within var ageDifference)don't take capital letters? Can anyone enlighten me?

function Person (name, age) {
    this.name = name;
    this.age = age;

// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
    return person1.age - person2.age;

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

// get the difference in age between alice and billy using our function
var diff = ageDifference(alice,billy);


It is normal convention for authors to write Class Definitions with a capital letter, and all other function definitions with lowercase, camelCase, or snake_case, all beginning with a lowercase letter. In respect of this convention, we never write variables or function names beginning with a capital letter. Custom object constructors are the exception since they are class definitions (blueprints of instance objects).

We will occasionally run across all capitals in a name. By convention these indicate a CONSTANT (generally global) that does not change during the run of the program.

Eg. Math.PI and Math.E are both numeric constants whose value never changes.

It is also worth noting that JavaScript (as most languages in general) is case sensitive. Person and person are spelled the same but to the interpreter they are two distinct variables that are not interchangeable.


Thankyou! Great reply, very helpful indeed!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.