Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: JavaScript 24-Hour Trainer
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: JavaScript 24-Hour Trainer
This is the forum to discuss the Wrox book JavaScript 24-Hour Trainer Jeremy McPeak; ISBN: 978-0-470-64783-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: JavaScript 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 19th, 2012, 04:30 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default Lesson 9 - prototypes

Hello
I have a question on prototypes. On the DVD, you say that the "this.getFullName = function(){ return this.firstName + this.lastName; }" is better to put it on the Person(firstName,lastName) prototype so we don't have to recreate it every time an instance of this object is created.
My question is, if we can do the same with a property of an object. For example if we want to give Person(firstName,lastName) an attribute numberOfLegs = 2
it make sense that all instances share the same attribute; there is no point to recreate the same attribute for every instance. If we do that it is shared among all the instances of that object? Does it works like a static attribute/variable in this case?
Thank you in advance
Reply With Quote
  #2 (permalink)  
Old March 22nd, 2012, 12:12 PM
jmcpeak's Avatar
Wrox Author
Points: 467, Level: 7
Points: 467, Level: 7 Points: 467, Level: 7 Points: 467, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2005
Location: , Texas, USA.
Posts: 87
Thanks: 0
Thanked 17 Times in 16 Posts
Default

Howdy, E01T.

Yes, you can put properties on the prototype, and the numberOfLegs property would be a good candidate for the prototype. But some Person instances might not have 2, so the constructor would need to be adjusted:

Code:
function Person(firstName, lastName, legs) {
    this.firstName = firstName;
    this.lastName = lastName;

    if (legs) {
        this.numberOfLefts = legs;
    }
}
Reply With Quote
The Following User Says Thank You to jmcpeak For This Useful Post:
E01T (April 10th, 2012)
  #3 (permalink)  
Old April 15th, 2015, 11:18 AM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2015
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default Lesson 9 and Creating an Object

Hello,

I was wondering how necessary this piece of code is in the grand scheme of things:

Code:
Employee.prototype = new Person();
As, the next line:

Code:
Employee.prototype.getFullName = function() {
    var fullName = Person.prototype.getFullName.call(this);

    return fullName + ", " + this.position;
}
seems to create the employee object and output the correct data.
Thank you for your time.
Reply With Quote
  #4 (permalink)  
Old April 15th, 2015, 01:18 PM
jmcpeak's Avatar
Wrox Author
Points: 467, Level: 7
Points: 467, Level: 7 Points: 467, Level: 7 Points: 467, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2005
Location: , Texas, USA.
Posts: 87
Thanks: 0
Thanked 17 Times in 16 Posts
Default

Hello, Coda.

Lesson 9 discusses the concept on inheritance in JavaScript. Inheritance in JavaScript is achieved by chaining one object's prototype with another object's prototype. So from that standpoint, the line:

Code:
Employee.prototype = new Person();
is extremely important because it sets up the prototype chain between Employee and Person. These days, I'd write it as:

Code:
Employee.prototype = Object.create(Person.prototype);
However, from a purely functional standpoint, you don't have to establish the Employee/Person prototype chain in order for Employee's getFullName() to work (as you noticed). And in hindsight, the example in Lesson 9 doesn't demonstrate the benefits of inheritance. So consider this:

Code:
function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

Person.prototype.getFullName = function() {
    return this.firstName + " " + this.lastName;
};

function Employee(firstName, lastName, position) {
    Person.call(this, firstName, lastName);
    this.position = position;
}

Employee.prototype = Object.create(Person.prototype);

Employee.prototype.getEmployeeInfo = function() {
  return this.getFullName() + ", " + this.position;
};

var employee = new Employee("John", "Doe", "Manager");

var info = employee.getEmployeeInfo();
You can run this at http://jsfiddle.net/LhLw66ue/. Two important things to note in this new code:

- getFullName() is no longer defined on Employee.prototype.
- A new getEmployeeInfo() method is defined on Employee.prototype.

Let's focus on the getEmployeeInfo() method:

Code:
Employee.prototype.getEmployeeInfo = function() {
  return this.getFullName() + ", " + this.position;
};
This code calls the getFullName() method. This works because we set up the Employee/Person prototype chain. Without it, we'd get an error when executing getEmployeeInfo().
Reply With Quote
The Following User Says Thank You to jmcpeak For This Useful Post:
Coda (April 15th, 2015)
  #5 (permalink)  
Old April 15th, 2015, 04:34 PM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2015
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default Lesson 9 and Creating an Object

Hello Mr McPeak,

my apologies for not using the quick reply - for some reason, it wouldn't work.

Thank you for your reply. The reason I asked about the importance of the said line of code was really looking at efficiency. I thought the said line seemed somehow redundant, but I did not fully appreciate it's relationship to the lesson overall.

Your extended example is a little confusing, so I will need to spend a little more time with it before I fully comprehend it; nonetheless, thank you for the explanation.

As a post-script, could you just clarify what is meant by 'returning the function to the caller', as every time I think I know what it means, something else is said that confuses me again.

Last edited by Coda; April 15th, 2015 at 04:36 PM.
Reply With Quote
  #6 (permalink)  
Old April 15th, 2015, 05:13 PM
jmcpeak's Avatar
Wrox Author
Points: 467, Level: 7
Points: 467, Level: 7 Points: 467, Level: 7 Points: 467, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2005
Location: , Texas, USA.
Posts: 87
Thanks: 0
Thanked 17 Times in 16 Posts
Default

Howdy, Coda.

The key to the newer example is the contents of the getEmployeeInfo() method.

Code:
this.getFullName() + ", " + this.position;
The first part of the statement is this.getFullName(). So the JavaScript engine goes through a lookup process trying to find the getFullName() method.

1 - It first looks at the object referenced by this--which in this case is employee. It won't find getFullName() on the employee object because we didn't define it there.

2 - The JavaScript engine then looks for getFullName() on Employee.prototype. Once again, it won't find the method because we didn't define it on Employee.prototype.

3 - The JavaScript engine will then search Person.prototype for getFullName() because it is the next link in the prototype chain. The getFullName() is defined here, so the JavaScript engine executes the method. It returns "John Doe".

At this point, you essentially have this:

Code:
"John Doe" + ", " + this.position;
which is simple concatenation. The result is:

Code:
"John Doe, Manager"
The phrase "return to the caller" typically means that a function exits and returns a value to whatever called it.
Reply With Quote
The Following User Says Thank You to jmcpeak For This Useful Post:
Coda (April 16th, 2015)
  #7 (permalink)  
Old April 16th, 2015, 07:45 AM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2015
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Mr McPeak,

thank you for elaborating on the code. It makes sense; now I have to practice it.
Reply With Quote
  #8 (permalink)  
Old April 22nd, 2015, 04:09 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2014
Location: Bangladesh
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Arrow autos brunei

We are working hard to make this site a great place for
new and used cars in Brunei.  We know how hard it can
be to find or sell a car and wanted to provide an really great
way of viewing and sharing information about cars
that are for sale.  If you have suggestions for features you would like
to see on the site or any other feedback please don't hesitate to let us know. 
Reply With Quote
  #9 (permalink)  
Old April 22nd, 2015, 04:30 PM
Registered User
Points: 4, Level: 1
Points: 4, Level: 1 Points: 4, Level: 1 Points: 4, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2015
Location: Bangladesh
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Arrow autos brunei

We are working hard to make this site a great place for
new and used cars in Brunei.  We know how hard it can
be to find or sell a car and wanted to provide an really great
way of viewing and sharing information about cars
that are for sale.  If you have suggestions for features you would like
to see on the site or any other feedback please don't hesitate to let us know. 
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 8 - Try It Femi Sonuga BOOK: Knight's Microsoft Business Intelligence 24-Hour Trainer 4 February 12th, 2012 08:44 AM
Lesson 7 Joakim BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 November 30th, 2011 11:16 AM
Lesson 4 Femi Sonuga BOOK: Knight's Microsoft Business Intelligence 24-Hour Trainer 0 November 25th, 2011 10:25 AM
Lesson 4 - Try It brookesmith BOOK: JavaScript 24-Hour Trainer 2 September 15th, 2011 06:40 PM
Lesson 4 Mtheys BOOK: Java Programming 24-Hour Trainer by Yakov Fain 3 September 15th, 2011 06:02 AM



All times are GMT -4. The time now is 01:11 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.