Javascript question

I am new to javascript and in one of my lecture, there is one question that I couldn't solve or understand how to solve it. I have to fill in the code in Sample in order to have a matching output. I tried to create
mostRecentSample = new Sample(first, last);

Open in new window

but it cause too much recursion error. Any suggestion? If you can come up with a solution, please explain why it work
var Sample = function(first, last) {
   //code in here
};

var main = function() {
   var sample = new Sample("John", "Doe");

   sample.print();
   if (Sample.mostRecent === sample) 
   {
      print("In comparation");
      Sample.mostRecent.print();
   }
   
   sample = new Sample("Jane", "Smith");
   sample.print();
   
   if (Sample.mostRecent === sample) 
   {
      print("In comparation");
      Sample.mostRecent.print();
   }
};

main();

Open in new window


/*Output
Doe, John
Doe, John
Smith, Jane
Smith, Jane
*/
LVL 1
Dung TrinhMarching Band Field CrewAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
Here's your solution: http://jsfiddle.net/AlexCode/3nxg0kfc/

Basically you just need to implement the print function in the sample 'class'.
When you do new of a function, the result is an object, and anything you assign to 'this', will be properties of that object.
So in my code I create the print function assigning it to 'this'.

var Sample = function(first, last) {    
    this.print = function(){
        console.log(last + ', ' + first);
    };
    
    Sample.constructor.prototype.mostRecent = this;
};

var main = function() {
   var sample = new Sample("John", "Doe");

   sample.print();
   if (Sample.mostRecent === sample) 
   {
      Sample.mostRecent.print();
   }
   
   sample = new Sample("Jane", "Smith");
   sample.print();
   
   if (Sample.mostRecent === sample) 
   {
      Sample.mostRecent.print();
   }
};

main();

Open in new window

Although I just not sure about the inner code there, calling a print("In comparation");
Was it you that added this code here? Because dispite of the english typo, print() is the same as window.print() which call the printer dialog. So I will ignore this and remove it from the code.

Now for the tricky part:
if (Sample.mostRecent === sample)
First of all, Sample is a function, and sample is an object. JavaScript is case sensitive and playing with it like this can lead to some misunderstandings.
So if Sample is a function, to have this mostRecent property added to a function definition directly, you have to add it to its prototype. Notice that we're not calling the Sample function here, we're merely using its definition.
This is quite advanced stuff, so if you're starting to learn JavaScript, you should start with the basics first.
Although the code that does the magic is the following:
Sample.constructor.prototype.mostRecent = this;

Open in new window

Again, this is quite advanced stuff here, and there's a lot going on that needs a quite deep understanding of the language.
I don't know what you're reading to learn JavaScript but I strongly recommend the following books:
Secrets of a JavaScript Ninja
High Performance JavaScript
These are the two "must read" JavaScript books. They start slow and grow even beyond the complexity of what you have in this sample.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dung TrinhMarching Band Field CrewAuthor Commented:
Thank you. Yes. the part ("In comparation") is put in just to debug.
I am taking a introduction to javascript class. I am using the book "Javascript, The Good part."
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.