-5

I have looked at a number of tutorials but I'm still struggling with the following code, can someone please explain this code. Can you please explain what each part is and what oit does.

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


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 = 
nnnnnn
  • 138,378
  • 23
  • 180
  • 229
Wer Erew
  • 31
  • 1
  • 1
  • Check out this tutorial http://www.codeproject.com/Articles/22769/Introduction-to-Object-Oriented-Programming-Concep and read points 4.4 and 4.5. Those should already explain what happens in this code. – Bas Jansen Jul 17 '13 at 12:54
  • There is missing something like `ageDifference(alice, billy);` at the end. – Niko Jul 17 '13 at 12:54
  • That's not very _object oriented_ to me. – moonwave99 Jul 17 '13 at 12:54
  • this is what i am trying to do? – Wer Erew Jul 17 '13 at 13:00
  • We have created two example people, alice and billy. Complete line 17 by calling ageDifference and saving the result in our global diff variable. – Wer Erew Jul 17 '13 at 13:01
  • He isn't asking for code, he is asking for an explanation of code and how it relates to object-oriented programming, a valid question and something that is very complicated and deserves attention. – Ilan Biala Jul 17 '13 at 14:21

3 Answers3

2
function Person(name, age) { // define a _constructor_ called _Person_
    this.name = name;        // constructor sets instance property _name_
    this.age = age;          // constructor sets instance property _age_
} // when invoked with _new_, this creates an..
  // ..Object which is an instance of Person

var ageDifference = function (person1, person2) { // a function (expression)
    return person1.age - person2.age;  // which returns the difference.. 
}                                      // ..between _age_ properties

var alice = new Person("Alice", 30); // new instance of _Person_
var billy = new Person("Billy", 25); // another new instance
/* _alice_ now looks like    |    _billy_ now looks like
     {                       |      {
         age: 30,            |          age: 25,
         name: "Alice"       |          name: "Billy"
     }                       |      }
*/
// get the difference in age between alice and billy using our function
var diff = ageDifference(alice, billy); // passing the instances from above..
                                        // ..into the function expression
Paul S.
  • 58,277
  • 8
  • 106
  • 120
0

javascript is not the best language to write object oriented. Basicly wat what you do is: you create a new object with the new operator.

What is the 'new' keyword in JavaScript?

all the this.[variable] setters will be stored in that object. so when you call new Person all the this assignments in function will be stored as an object, so this object (which is like a map) will have the keys name and age with the assigned values.

so alice will be { name : "Alice", age : 30 } and billy will be { name : "Billy", age : 25 }

cause javascript doesn't type check you can simply do: ageDifference({age: 20},{age: 30}); and it will still work. Thats why javascript is not the best object oriented language.

(you could use typeScript to write javascript object oriented: http://www.typescriptlang.org/)

Community
  • 1
  • 1
Joel Harkes
  • 9,189
  • 2
  • 41
  • 58
0

This code shows a contsructor at the top, which is used to create Person objects by providing the parameters name and age. This object can be given more properties, such as hair color, height, weight, and much more. By using these objects, you can simply store all the objects in an array or list and this will provide for a psuedo database that could be use for the census bureau to keep track of people. Object-oriented programming is all about creating objects to store data instead of relying mainly on variables with each person's name before the variable.

For example, instead of:

var ericName = 'Eric';
var ericAge = '22';

You could do:

var eric = new Person('Eric', '22');

and this will create a Person object stored in the variable eric. His age would be accessed by calling eric.name and eric.age. Object-oriented programming simplifies having so many variables into having just a few and having lots of methods to access their information and change it.

The next segment:

var ageDifference = function(person1, person2) {
    return person1.age - person2.age;
}

should actually be rewritten as:

function getAgeDifference(person1, person2) {
    return person1.age - person2.age;
}

To get the age difference between two people, you would call getAgeDifference(alice, billy);. This calls the getAgeDifference method that will use their age properties to provide you with more information.

Object-oriented languages such as Java, C++, Objective-C, also make use of classes that help separate sections of relevant code into different classes or files. This idea of classes is very helpful yet hard to understand and if you are looking to learn a lot about it I would recommend picking up a book about a specific language and going along with the information it provides to give you an idea of how to write object-oriented code and some examples and guides of what to do each step of the way.

Good luck!

Ilan Biala
  • 3,093
  • 5
  • 32
  • 45
  • `should actually be rewritten as` why **should** it? Is there reasoning behind this? What is wrong with a _function expression_, especially in this context where you may (in the next step of learning) be going on to _prototypes_? – Paul S. Jul 17 '13 at 13:10
  • I learned Java as an object-oriented language that uses specific methods - accessor and mutator methods mainly - to interact with objects. Though you can store the result in a variable, usually I don't because I use it right away or wait to call the function until I need it. – Ilan Biala Jul 17 '13 at 14:23
  • Sorry, I did not understand "Though you can store the result in a variable". What result? The _function_ is being defined as an _expression_ with the reference being the variable, the differences between a _function expression_ and _function declaration_ mainly come down to when they may be invoked, and if it's after the definition, both act pretty much the same. The other main difference being you can assign properties of _Objects_ as functions directly when you use expressions, vs needing to make sure you don't have namespace conflicts and an extra assigning line for declarations – Paul S. Jul 17 '13 at 16:25