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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:13:39+00:00 2026-06-07T02:13:39+00:00

This is an interview questions, Asked to write a Man class to let the

  • 0

This is an interview questions, Asked to write a Man class to let the following code can run properly:

var me =new  Man();

//method one:
me.attr("fullname", "tom");

//method two:
me.fullname = "jim";

console.info("my name is:" + me.attr("fullname"));

/*------[result is]------
my name is:tom
------------------*/

my answer is:

var Man=function(){
};

Man.prototype.attr=function(attr,val){
    if(val){
        this[attr]=val;
    }
    else{
        return this[attr];
    }
}

The results of my code to run is:

/*------[result is]------
my name is:jim
------------------*/

who can help me? 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-07T02:13:41+00:00Added an answer on June 7, 2026 at 2:13 am

    You could make a second dictionary:

    function Man() {
        this.dictionary = {};
    }
    
    Man.prototype.attr = function(attr, val) {
        if(arguments.length > 1) {
            this.dictionary[attr] = val;
        } else {
            return this.dictionary[attr];
        }
    };
    

    You could use a closed variable:

    function Man() {
        var dictionary = {};
    
        this.attr = function(attr, val) {
            if(arguments.length > 1) {
                dictionary[attr] = val;
            } else {
                return dictionary[attr];
            }
        };
    }
    

    You could use a closed fullname variable, and ignore everything except 'fullname':

    function Man() {
        var fullname;
    
        this.attr = function(attr, val) {
            if(attr === 'fullname') {
                if(arguments.length > 1) {
                    fullname = val;
                } else {
                    return fullname;
                }
            }
        };
    }
    

    You could also return "tom" every single time, or pretend all the attributes are "fullname", or both. You could ROT13 property names before assigning them. You could add underscores. You could create a property instead that throws away values beginning with "j". The possibilities are limitless, actually.

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

Sidebar

Related Questions

I was asked this question in some interview. I was required to write code
This was the question asked in interview. Can we call one constructor from another
I was asked in an interview this question so I answered with the following:
In an interview this is the question that is asked: You have to write
This was one of the interview questions asked. How to find the length of
This were the questions I was asked in the interview fews days back and
I guess this is one of the most commonly asked interview questions, yet I
One of the interview questions asked me to write the prototype for a C
I was asked this in an interview today and I just can't figure it
This is an interview Question that i was asked recently: Write a C program

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.