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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:29:40+00:00 2026-05-12T00:29:40+00:00

It would be exceedingly handy if I could do this: var MyObject = function(param1,

  • 0

It would be exceedingly handy if I could do this:

var MyObject = function(param1, param2, ... paramN)
{
    this.var1 = stuff;
    this.var2 = moreStuff;
    .
    .
    .
    this.varN = nStuff;

    this.validate = function()
    {
        for(var current in this)
        {
            alert(current);
            //validate all member variables (even this function I suppose)
        }
    };
};

This however does not seem to do what I would want. I realize that the loop would eventually have to loop over it’s parent function (which also, not surprisingly, does not happen).

Is this impossible because the ‘this’ in the second function refers to the second function and not the first? Or is the keyword ‘this’ only a declaration operator for a public member and not a reference to the outer object ?

I figure getting what I want this way is not possible but is there another way I can go about achieving this behaviour ?

  • 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-12T00:29:41+00:00Added an answer on May 12, 2026 at 12:29 am

    I think you’re trying to get the value of the member and going about it the wrong way so try this:

          var MyObject = function() {
            this.var1 = 'var 1 value';
            this.var2 = 'var 2 value';
            this.varN = 'var n value';
            var self = this;
    
            this.validate = function() {
              for (var member in self) {
                if (!self.hasOwnProperty(member) || typeof(self[member]) === "function") continue;
                alert(self[member]);
              }
            };
          };
    
          var m = new MyObject();
          m.validate();
    

    To explain: the loop check first if the property is a user defined property as opposed to being inherited from the Object object. It also checks that the member is not a function (like validate()) it then alerts the value of the member.

    The hasownproperty check is recommended by Douglas Crockford (father of JS) as best practice when iterating over memebers.

    Hope this helps,

    Darko

    EDIT: Forgot to mention self – i included this because its the standard way of making sure that your this is actually what you want it to be.

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

Sidebar

Ask A Question

Stats

  • Questions 190k
  • Answers 190k
  • 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 transfer it as binary otherwise it will get corrupted as… May 12, 2026 at 6:03 pm
  • Editorial Team
    Editorial Team added an answer There's no global command/setting, I'm afraid. If you're generating scripts… May 12, 2026 at 6:03 pm
  • Editorial Team
    Editorial Team added an answer You can still have a custom initializer. However, inside this… May 12, 2026 at 6:03 pm

Related Questions

Is it possible to merge to a branch that is not a direct parent
I just fixed a memory leak caused by someone forgetting to call the superclass's
When designing LINQ classes using the LINQ to SQL designer I've sometimes needed to
Here's a simplified version of what I'm trying to do : Before any other

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.