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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:32:21+00:00 2026-06-18T09:32:21+00:00

We have been debating how best to handle objects in our JS app, studying

  • 0

We have been debating how best to handle objects in our JS app, studying Stoyan Stefanov’s book, reading endless SO posts on ‘new’, ‘this’, ‘prototype’, closures etc. (The fact that there are so many, and they have so many competing theories, suggests there is no completely obvious answer).

So let’s assume the we don’t care about private data. We are content to trust users and developers not to mess around in objects outside the ways we define.

Given this, what (other than it seeming to defy decades of OO style and history) would be wrong with this technique?

// namespace to isolate all PERSON's logic
var PERSON = {};

// return an object which should only ever contain data.
// The Catch: it's 100% public

PERSON.constructor = function (name) {
     return {
         name: name
     } 
}

// methods that operate on a Person
// the thing we're operating on gets passed in

PERSON.sayHello = function (person) {
     alert (person.name); 
}

var p = PERSON.constructor ("Fred");
var q = PERSON.constructor ("Me");

// normally this coded like 'p.sayHello()'
PERSON.sayHello(p);
PERSON.sayHello(q);

Obviously:

  1. There would be nothing to stop someone from mutating ‘p’ in unholy
    ways, or simply the logic of PERSON ending up spread all over the place. (That is true with the canonical ‘new’ technique as well).
  2. It would be a minor hassle to pass ‘p’ in to every function that you
    wanted to use it.
  3. This is a weird approach.

But are those good enough reasons to dismiss it? On the positive side:

  1. It is efficient, as (arguably) opposed to closures with repetitive function declaration.
  2. It seems very simple and understandable, as opposed to fiddling with
    ‘this’ everywhere.

The key point is the foregoing of privacy. I know I will get slammed for this, but, looking for any feedback. Cheers.

  • 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-18T09:32:22+00:00Added an answer on June 18, 2026 at 9:32 am

    There’s nothing inherently wrong with it. But it does forgo many advantages inherent in using Javascript’s prototype system.

    • Your object does not know anything about itself other than that it is an object literal. So instanceof will not help you to identify its origin. You’ll be stuck using only duck typing.
    • Your methods are essentially namespaced static functions, where you have to repeat yourself by passing in the object as the first argument. By having a prototyped object, you can take advantage of dynamic dispatch, so that p.sayHello() can do different things for PERSON or ANIMAL depending on the type Javascript knows about. This is a form of polymorphism. Your approach requires you to name (and possibly make a mistake about) the type each time you call a method.
    • You don’t actually need a constructor function, since functions are already objects. Your PERSON variable may as well be the constructor function.

    What you’ve done here is create a module pattern (like a namespace).

    Here is another pattern that keeps what you have but supplies the above advantages:

    function Person(name)
    {
        var p = Object.create(Person.prototype);
        p.name = name; // or other means of initialization, use of overloaded arguments, etc.
        return p;
    }
    Person.prototype.sayHello = function () { alert (this.name); }
    
    var p = Person("Fred"); // you can omit "new"
    var q = Person("Me");
    p.sayHello();
    q.sayHello();
    console.log(p instanceof Person); // true
    var people = ["Bob", "Will", "Mary", "Alandra"].map(Person);
            // people contains array of Person objects
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently our production environment runs JBoss 5.1 and we have been debating whether or
I have been debating using css with div's and laying out elements, and really
Have been reading about async and tasks and been attempting to convert the CopyFileEx
I have been debating whether or not to use ASP.Net WinForms to manage content
I have been reading up on password hashing, but all the forums I read
Have been reading up and checking other questions, but don't understand / can't seem
I have been doing a lot of reading and tutorials on Dependency Injection via
i have been debating which OS to use for a car pc.. and ubuntu
I have been intending to use django-notification in my django app. The docs are
Have been studying the file system related classes of Adobe AIR 1.5, but so

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.