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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:30:47+00:00 2026-05-22T22:30:47+00:00

I know you can create literal objects with subobjects and functions: var obj =

  • 0

I know you can create literal objects with subobjects and functions:

var obj = {
    val : 1,
    level1 : {
        val : 2,
        val2 : 3,
        func : function(){
            return this.val2
        }
    }
}

console.log(obj.val);
console.log(obj.level1.val);
console.log(obj.level1.func());

outputs:

1
2
3

What I would like to do is do the same thing with methods in a object, something similar to:

function objType() {
    this.val = 1;
    this.func = function(){
        return this.val;
    }
    this.level1 = function(){
        this.val = 2;
        this.func = function(){
            return this.val;
        }
        this.level2 = function(){
            this.val = 3;
            this.func = function(){
                return this.val;
            }
        }
    };
};

then i would expect:

var obj = new objType();
console.log(obj.func());
console.log(obj.level1.func());
console.log(obj.level1.level.func());

to output:

1
2
3

However, only the first console.log outputs before the script throws an error.

Is there any way to have sub methods in Javascript?

–edit–

My goal is to create a class i can use for showing a box in the middle of the screen, for displaying messages, questions(to get a yes/no response), and forms. I was thinking a good way to structure it would be with sub methods, so that it could then be referenced with:

function box() {
    this.show = function(){
        //multiple sub methods here
    }
    this.hide = function(){
        //code to hide window here
    }
}

var aBox = new box();
aBox.show.message('the message');
aBox.hide();
aBox.show.question('the question');
aBox.hide();

–edit–
thanks @Sean Vieira

For completeness I’ll put the modified version of my code here using his solution:

function objType() {
    this.val = 1;
    this.func = function(){
        return this.val;
    }
    this.level1 = {
        val : 2,
        func : function(){
            return this.val;
        },
        level2 : {
            val : 3,
            func : function(){
                return this.val;
            }
        }
    }

var obj = new objType();
console.log(obj.func());
console.log(obj.level1.func());
console.log(obj.level1.level.func());

that outputs

1
2
3
  • 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-22T22:30:47+00:00Added an answer on May 22, 2026 at 10:30 pm

    you can do this easily with chaining

    function Box() {
        this.show = function(){
           //your code goes here
           return this;
    
        },
        this.message = function(message){
           //code goes here
           return this;
        }
      }
    
    var aBox = new Box();
    aBox.message('the message').show()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using python 3.1.1. I know that I can create byte objects using the
I know I can create a verbatim string literal in C# by using the
I know I can create a tcp server like that in node.js var dataServer
I want to know if i can create a custom google maps application,on which
I need to know if I can create a file in a specific folder,
this question can create a misunderstanding: I know I have to use CSS to
I know how to create a custom user control in WPF but how can
Does anyone know of any software that can be used to create Agile User
I think I know how to create custom encrypted RSA keys, but how can
Can PL/SQL procedure in Oracle know it's own name? Let me explain: CREATE OR

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.