Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 113659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:44:14+00:00 2026-05-11T02:44:14+00:00

I’ve been using javascript for a while, but have never learned the language past

  • 0

I’ve been using javascript for a while, but have never learned the language past the basics. I am reading John Resig’s ‘Pro Javascript Techniques’ – I’m coming up with some questions, but I’m not finding the answers to them in the book or on google, etc.

John gives this example in his book:
Function #1

function User( name, age ){   this.name = name;   this.age = age; } // Add a new function to the object prototype User.prototype.getName = function(){   return this.name; }; User.prototype.getAge = function(){   return this.age; }; var user = new User( 'Bob', 44 ); console.log('User: ' + user.getName() + ', Age: ' + user.getAge()); 

I’m still learning about the prototype property, so I tried writing something similar:
Function #2

function User (name, age ) {   this.name = name;   this.age = age;   this.getName = function() {     return this.name;   };   this.getAge = function() {     return this.age;   }; } var user = new User( 'Bob', 44 ); console.log('User: ' + user.getName() + ', Age: ' + user.getAge()); 

It doesn’t use the prototype property to create the getName and getAge functions, but the output is the same as John’s example.

I took it one step further, and created this:
Function #3

var User = {   name: '',   age: 0,   setName: function(name) {     this.name = name;   },   setAge: function(age) {     this.age = age;   },   getName: function() {     return this.name;   },   getAge: function() {     return this.age;   } }; User.setName('Bob'); User.setAge(44); console.log('User: ' + User.getName() + ', Age: ' + User.getAge()); 

Again – it looks different than John’s example (and I had to add setter methods), but the output is the same.

Question #1 – what is the difference between the 3 functions? What is the advantage of the prototype property, and is Function #2 doing anything incorrectly, because it seems more straight forward to code #2 instead of #1 (although I’m sure #1 is doing it better seeing as John created it).

Question #2 – How could I modify function #3 to not use the setName and setAge methods, but still keep the {…} shorthand? Can the {…} shorthand have constructors?

Thanks in advance for helping me learn!

EDIT I think my 2nd question was a little confusing. I meant how could I use the {…} shorthand to create a User object, but then after I create the object, say something like:

var user = new User('Bob', 44); 

Just like in Function #1 – or is that not possible?

EDIT #2 Wow! Thanks everyone for the awesome answers. That really makes it a lot more clear to me. So if I understand correctly, the difference between #1 and #2 aren’t too much. If I only ever create one ‘User’ object – they probably aren’t different at all. But if my program creates many User objects, #1 would most likely be more efficient and use less memory since all objects will share the same functions.

I really appreciate all of the great answers – Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:44:15+00:00Added an answer on May 11, 2026 at 2:44 am

    Every time a function() {} is evaluated, it creates a new function object. Therefore, in #1 all of the User objects are sharing the same getName and getAge functions, but in #2 and #3, each object has its own copy of getName and getAge. All of the different getName functions all behave exactly the same, so you can’t see any difference in the output.

    The {…} shorthand is a constructor. When evaluated, it constructs a new ‘Object’ with the given properties. When you run ‘new User(…)’, it constructs a new ‘User’. You happen to have created an Object with the same behavior as a User, but they are of different types.

    Response to comment:

    You can’t, directly. You could make a function that creates a new object as per #3. For example:

    function make_user(name, age) {     return {         name: name,         age: age,         getName: function() { return name; },         getAge: function() { return age; },     }; }  var user = make_user('Joe', '18'); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, UrlClassLoader always tries to find classes based in the… May 11, 2026 at 4:59 pm
  • Editorial Team
    Editorial Team added an answer Maven is following the "Convention over Configuration" principle, which means… May 11, 2026 at 4:59 pm
  • Editorial Team
    Editorial Team added an answer Actually, each property in the resulting anonymous class needs a… May 11, 2026 at 4:59 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.