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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:31:43+00:00 2026-06-02T08:31:43+00:00

I’m working on a fairly complex object in JS and I’m running into issues:

  • 0

I’m working on a fairly complex object in JS and I’m running into issues:

I have the following (abridged) code:

var LocationSelector;

LocationSelector = function(container) {
  this.selectors = {
    container: container,
    city_name: container.find('input.city_name'),
    city_id: container.find('input.city_id')
  };
  return this.initialize();
};

LocationSelector.prototype = {
  initialize: function() {
    return this.city.checkStatus();
  },
  city: {
    status: null,
    message: null,
    id: null,
    checkStatus: function() {
      if (LocationSelector.selectors.city_name.val() && LocationSelector.selectors.city_id.val()) {
        return LocationSelector.city.setStatus('success');
      }
    },
    setStatus: function(status) {
      return alert(status);
    }
  }
};

Two questions:

1) Inside of a sub-object function this no longer refers back to the root object. It seems I can refer back to the parent if I write out LocationSelector.object.method( args ), but that’s a lot to type. Is there a way to define a shortcut back to the parent object?

2) In some situations I need to have several instances of this per page, so it’s important to me that I can set the various selectors when a new object is instantiated and then refer to the instance selectors in the prototype. Is referring to the parent object (ie. LocationSelector) in sub-object methods even viable for that? How does JS know to stay with the currently active object’s stored properties?

Basically, I’m trying to implement a class, and I’m totally new to JS and don’t really know how to do it. So, any help or suggestions are appreciated. 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. Editorial Team
    Editorial Team
    2026-06-02T08:31:47+00:00Added an answer on June 2, 2026 at 8:31 am

    There are many things wrong with your current approach. Here is something closer to what you want, although I do not understand why LocationSelector instances have a city member.

    function LocationSelector(container) {
      this.selectors = {
        container: container,
        city_name: container.find("input.city_name"),
        city_id: container.find("input.city_id")
      };
    
      this.city = new City(this);
      this.city.checkStatus();
    }
    
    function City(locationSelector) {
      this.status = null;
      this.message = null;
      this.id = null;
      this.locationSelector = locationSelector;
    }
    
    City.prototype.checkStatus = function () {
      if (this.locationSelector.selectors.city_name.val() && this.locationSelector.selectors.city_id.val()) {
        this.setStatus("success");
      }
    };
    
    City.prototype.setStatus = function () {
      alert("status");
    };
    

    Things to note:

    1. Data properties go on the instance, not the prototype. Only methods go on the prototype.
    2. City is clearly its own class, so you should make it one. In your code, a single city is being shared between all instances of LocationSelector, since it is put on the prototype. In this code, it is assigned as an instance property, in the LocationSelector constructor.
    3. You cannot reference LocationSelector.selectors like you do in your example. LocationSelector.selectors would be for “static” properties, which LocationSelector does not have. Instead you need to refer to the selectors property on specific instances; in this example, that instance is given by this.locationSelector.
    4. Points 2 and 3 speak to an important fact: the “child” City instance cannot reference to properties of the “parent” LocationSelector class without having a concrete instance of it.

    Here is a version of the code that makes more sense to me, removing the part where LocationSelector has a city property (which it doesn’t use).

    function LocationSelectors(container) {
      this.city_name = container.find("input.city_name");
      this.city_id = container.find("input.city_id");
    }
    
    function City(locationSelectors) {
      this.locationSelector = locationSelector;
    }
    
    City.prototype.checkStatus = function () {
      if (this.locationSelectors.city_name.val() && this.locationSelectors.city_id.val()) {
        this.setStatus("success");
      }
    };
    
    City.prototype.setStatus = function () {
      alert("status");
    };
    
    function checkCityStatus(container) {
      var locationSelectors = new LocationSelectors(container);
      var city = new City(locationSelectors);
    
      city.checkStatus();
    }
    

    I leave you with a link to Crockford’s “Private Members in JavaScript”, which talks about doing OO in JavaScript. There are other, probably better explanations out there, but at least that one will put you on the right track.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am currently running into a problem where an element is coming back from
I have just tried to save a simple *.rtf file with some websites and

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.