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 8166619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:56:19+00:00 2026-06-06T19:56:19+00:00

I am new to Javascript and object-oriented programming in general. I would like to

  • 0

I am new to Javascript and object-oriented programming in general. I would like to know if I am following the best practices when writing JS OOP code.

Here I made a class called _name and gave it some properties as well as an object this.details. Then I use prototyping to create methods for the class.

//define _name class (I use _ to easily recognize classes)
function _name () {
    this.firstName = '';
    this.lastName = '';
    this.middleName = '';
    this.details = {
        eyeColor: '',
        hairColor: ''
    }
}

//begin _name methods

_name.prototype.getFullName = function() {
    return this.firstName + ' ' + this.middleName + ' ' + this.lastName;
}
_name.prototype.setFirstName = function(firstName) {
    if ($.trim(firstName).length && typeof firstName != 'not_defined') {
        this.firstName = firstName;
    } else {
        alert('Please enter a valid first name.');
    }
}
_name.prototype.setLastName = function(lastName) {
    if ($.trim(lastName).length && typeof lastName != 'not_defined') {
        this.lastName = lastName;
    } else {
        alert('Please enter a valid last name.');
    }
}
_name.prototype.setMiddleName = function(middleName) {
    if ($.trim(middleName).length && typeof middleName != 'not_defined') {
        this.middleName = middleName;
    } else {
        alert('Please enter a valid middle name.');
    }
}
_name.prototype.setHairColor = function(hairColor) {
    this.details.hairColor = hairColor;
}
_name.prototype.setEyeColor = function(eyeColor) {
    this.details.eyeColor = eyeColor;
}

//end _name methods

var personOne = new _name();
personOne.setFirstName('John');
personOne.setLastName('Doe');
personOne.setMiddleName('Barry');
personOne.setEyeColor('Brown');
personOne.setHairColor('Black');
document.write(personOne.getFullName());
document.write(personOne.details.eyeColor);
document.write(personOne.details.hairColor);
  • 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. Editorial Team
    Editorial Team
    2026-06-06T19:56:22+00:00Added an answer on June 6, 2026 at 7:56 pm

    In the big picture, yes, you’re doing fine; nice one. 🙂 Consider the examples in this other answer on SO for more information and perspective.

    Some pointers, none of which is OOP-related, just JavaScript-related:

    1. The overwhelming convention in JavaScript is that constructor functions (your _name) are initially-capped, e.g. Name (like Date or RegExp).

    2. Your various functions assigned in the form _name.prototype.setLastName = function() { ... } are anonymous functions assigned to properties that have names. Giving your functions proper names helps your tools help you. Some engines are smart enough to figure it out even when you don’t give your functions names, but others need proper names. See the link above for examples, and/or Anonymouses anonymous.

    3. You’re relying on the horror that is automatic semicolon insertion in all of your function assignments: _name.prototype.setLastName = function() { ... } should end with a semicolon after the }. Recommend learning the rules and always supplying the semicolons explicitly; when the engine has to guess, it can guess wrong, and it makes minification/compression/packing difficult when you leave them out.

    4. Rather than writing _name.prototype.xyz = ... all over the place, consider using a scoping function and caching _name.prototype to a simpler symbol, e.g.:

      (function(p) {
          p.setLastName = function() {
              // ...
          };
      
          p.xyz = function() {
              // ...
          };
      })(_name.prototype);
      

      …or using a helper function that goes further than that. Just to cut down on keystrokes and script size.

    But again, by and large, yes, you’re on the right track.

    More reading:

    • Mythical methods
    • You must remember this
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm somewhat new to object oriented programming in Javascript and I'm trying to build
Let's say I instantiate an object in Javascript like this: var myObj = new
I'm a relatively newbie to object oriented programming in JavaScript, and I'm unsure of
I have the following javascript object, somewhat pseudocode: { dateField: new Date(), addMinutes:function(numMinutes) {
I have a javascript object that I would like to update with data from
If I have a javascript object that looks like the following: { 0: [0,50],
i am new to object oriented javascript. I have a variable whose value i
I am new to object-oriented JavaScript. Currently, I have an object that I need
I'm new to object oriented javascript. I have a set up method that I
So i am trying to learn object oriented programming in javascript. function doStock() {

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.