-1

I'm learning Javascript in code academy. And I found this code:

// Our person constructor
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);

And it works and returns the difference. However I was wondering why inputting Person.alice and Person.billy into ageDifference returns an error. Isn't the age stored as Person.billy.age and Person.alice.age?

asked May 28, 2017 at 13:25
1
  • 2
    alice and billy are object names and they have nothing to do with inner structure of the object. So Person.billy and Person.alice reference to non existing fields of Person class (i.e. prototype in JavaScript). Commented May 28, 2017 at 13:29

3 Answers 3

2

Short answer, no. Person is the prototype (the analogue-but-not-same as class) for Alice and Billy. It defines the methods and fields a new object of this class can use, and is not a container for all Person objects as you imply.

The prototype, Person is by itself an object with its own fields and methods. Person.alice alludes to there being a "class field" called "alice", all Person objects will share - and of course has nothing to do with the new object you defined which happens to be stored in the variable "alice".

Bottom line, Person.alice/.billy are both undefined, and undefined.age is throwing an error.

answered May 28, 2017 at 13:29
Sign up to request clarification or add additional context in comments.

Comments

0

You can think of the function

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

when used with new, as a Mold. It does not contain the new object, just builds it.

answered May 28, 2017 at 13:57

Comments

0

Person is just schema how to create object it is not like in normal programing languages .. Some people say the Javascript is real OOP language , because every type is object , and every function is object ... there are no classes (schemas how to create some object) , shcema for creating some object is object not class... And if you want to simulate some oop concpet you must play with prototype .. its bit wierd but prototypes are like extensions of some object , like you say .. this object will point on this location and take all code where i poiting too

answered May 28, 2017 at 14:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.