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

  • Home
  • SEARCH
  • 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 514777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:33:36+00:00 2026-05-13T07:33:36+00:00

Just for the kicks i am trying to create a simple data object in

  • 0

Just for the kicks i am trying to create a simple data object in javascript. Here is the code.

    var roverObject = function(){

        var newRover = {};
        var name;
        var xCord;
        var ycord;
        var direction;

        newRover.setName = function(newName) {
            name = newName;
        };

        newRover.getName = function() {
            return name;
        };

        newRover.setDirection = function(newDirection) {
            direction = newDirection;
        };

        newRover.getDirection = function() {
            return direction;
        };

        newRover.setXCord = function(newXCord) {
            xCord = newXCord;
        };

        newRover.getXCord = function() {
            return xCord;
        };

        newRover.setYCord = function(newYCord) {
            yCord = newYCord;
        };

        newRover.getYCord = function() {
            return yCord;
        };

        newRover.where = function(){
            return "Rover :: "+ name +" is at Location("+xCord+","+yCord+") pointing to "+direction;
        };

        return newRover;
    };


    rover1 = new roverObject();
    rover2 = new roverObject();     

    rover1.setName("Mars Rover");
    rover1.setDirection("NORTH");
    rover1.setXCord(2);
    rover1.setYCord(2);
    console.log(rover1.where());
    console.log(rover1);

    rover2.setName("Moon Rover");
    rover2.setDirection("SOUTH");       
    rover2.setXCord(1);
    rover2.setYCord(1);     
    console.log(rover2.where());        
    console.log(rover2); 

There are few questions that I have around this creation.

  1. I want to create an object where the properties/attributes of object are private and not visible to world. Am I successful in doing that? Can I really not access the object attributes?
  2. Is there a better way to create this kind of object?
  3. If I want to inherit this object, I should do a newObject.prototype = roverObjectwill that work? And will that make sense most of all.
  4. Finally I have a wierd problem. Notice the last method of objet “where” which returns a concatenated string. Here I tried following code instead.

            newRover.where = function(){
            return "Rover :: "+ name +" is at Location("+xCord+","+yCord+") pointing to "+direction;
        }();
    

and then did a following console.log

console.log(rover1.where);
console.log(rover2.where);

It threw following error for me:

cannot access optimized closure

Why would it say that? What am I doing wrong?

Thanks for all the help. Any review comments would be appreciated too!
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-05-13T07:33:36+00:00Added an answer on May 13, 2026 at 7:33 am

    Am I successful in doing that? Can I really not access the object attributes?

    Indeed. You don’t have object attributes, you have local variables in the roverObject function. Local variables can’t be accessed from outside, only from the functions inside the roverObject function that have a closure over them.

    That you are calling roverObject as a constructor, with new roverObject, is irrelevant, as you are returning a different object from the function. Saying var rover1= roverObject() without the new would do exactly the same thing. Notably the object returned by [new] roverObject is a plain Object as you created it from {}; rover1 instanceof roverObject is false.

    If you wanted instanceof to work, you would have to call with new, and use this instead of newRover in the constructor function.

    If I want to inherit this object, I should do a newObject.prototype = roverObject will that work? And will that make sense most of all.

    No. You currently have no allowance for prototyping. You are using a separate copy of each method for each instance of the roverObject. You can do certainly objects this way but it’s a different approach than prototyping. If you wanted to make something like a subclass of roverObject in the arrangement you have now, you’d say something like:

    function AdvancedRover() {
        var rover= new roverObject();
        rover.doResearch= function() {
            return rover.where()+' and is doing advanced research';
        };
        return rover;
    }
    

    Note since the ‘private’ local variables in the base class constructor really are private, even the subclass cannot get at them. There’s no ‘protected’.

    newRover.where = function(){ … }();

    What’s that trying to do? I can’t get the error you do; all the above does is assigns the string with the location to where (before the setter methods have been called, so it’s full of undefineds).

    Is there a better way to create this kind of object?

    Maybe. see this question for a discussion of class/instance strategies in JavaScript.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 412k
  • Answers 413k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, you can loop through the elements, get the span… May 15, 2026 at 8:16 am
  • Editorial Team
    Editorial Team added an answer try using SWFLoader May 15, 2026 at 8:16 am
  • Editorial Team
    Editorial Team added an answer Set the StoreGeneratedPattern attribute to "Identity" in your SSDL for… May 15, 2026 at 8:16 am

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.