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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:11:28+00:00 2026-05-16T20:11:28+00:00

A small question for versed javascript programmers: There are two ways to declare a

  • 0

A small question for versed javascript programmers: There are two ways to declare a function in javascript.

A: javascript-object-notation

function MyLittleHouseA(name) {
    this.age = 88,
    this.name = name,
    this.getName = function()
    {
        return this.name;
    }
}

B: uhm, normal:

function MyLittleHouseB(name) {
    this.age = 88;
    this.name = name;
    //do some more proceduaral stuff upon 'construction'
    this.age = this.age + 20; 
    this.getName = function()
    {
        return this.name;
    }
}

I find A more elegant (and have a number of objects I want to turn into configurable options…), but might want to do some more stuff upon instance creation, hence I do need as shown in B.

Can these be mixed?

¡ Thanx !

  • 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-16T20:11:29+00:00Added an answer on May 16, 2026 at 8:11 pm

    Your first option doesn’t use object notation. If you want to use object notation, you could write something like this:

    function MyLittleHouse(name) {
        var age = 88;
        age += 20;
        return {
            getName: function() {
                return name;
            }
        }
    }
    

    This has the advantage of not using this, which means you avoid any issues with the binding of this. It also hides age and name, which may or may not be desirable — as it stands, there’s no way for age to be accessed, while name is immutable since it can only be read via getName. If you want to expose age as a normal field:

    function MyLittleHouse(name) {
        var age = 88;
        age += 20;
        return {
            age: age,
            getName: function() {
                return name;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small question in JavaScript. Here is a declaration: function answerToLifeUniverseAndEverything() {
small question about C++ replace function. I'm parsing every line of text input line
I have small question by entity framework 4.1. There is interface of image DTO:
I've read all recommended articles on SynchronizationContext, but there is one small question I
I have a small question and I think this is asked before but I
I have a small question concerning css styles and javascript / jquery. Lets say
I have small question about pdp-11(simulator), I have this command (it begins from the
This is a very small question, and probably something really silly! But why am
I have a small question in passing two or more sys.argv arguments in Python.
I have a small question hoping someone will help me. Is there any way

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.