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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:03:10+00:00 2026-06-02T18:03:10+00:00

Relative JS newbie here trying to understand the difference between two ways of defining

  • 0

Relative JS newbie here trying to understand the difference between two ways of defining a JS object…

In certain cases I have seen examples of what I would call “namespace” objects or “handlers” that might look something like this:

var example = {
  message: null,
  setup: function( message ) {
    example.message = message;
    alert( message );
  }
}

I might use something like this to group some related functions. If I were to call example('foo!') I would expect an alert message with foo! to appear, and thereafer if I called example.message I would expect it to return foo!.

My first question is: what is this kind of object (not defined as a function) called?

I use objects like this a lot in my sites, as handlers to setup a bunch of bindings for ajax interactions etc. I’ve been trying to learn about object-oriented javascript, and have been doing some reading (1,2). In the Mozilla docs it’s suggested that objects should be defined as functions, so more like this:

var example = function( message ) {
    ....
}

My second question is: What is this kind of object (defined as a function) called?

I don’t really understand the difference between the two so I’m having a number of issues, which raises question 3:

If you define an object as a function, how do you define properties on the object if you don’t necessarily want them to execute when it is instantiated?

Ie. if I do this:

var example = function( message ) {
    alert('message');
}

Then I know that example('foo!') will trigger an alert with foo!. But, how do I define other properties or methods on the object to access and/or call later?

  • 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-02T18:03:10+00:00Added an answer on June 2, 2026 at 6:03 pm

    My first question is: what is this kind of object (not defined as a
    function) called?

    That is knows as object literal notation.

    My second question is: What is this kind of object (defined as a
    function) called?

    That is knows as function expression.

    More Readings:

    • Named function expressions demystified
    • Function Declarations vs. Function Expressions

    If you define an object as a function, how do you define properties on
    the object if you don’t necessarily want them to execute when it is
    instantiated?

    You can use this keyword to add properties or use prototype property like this:

    var example = function( message ) {
        alert('message');
        this.hello = function(){
            alert('hello'); 
        }
    }
    

    Or

    var example = function( message ) {
        alert('message');
    }
    
    example.prototype.hello = function(){ 
       alert('hello');
    }
    
    var e = new example('hello');
    e.hello();
    

    When using this, you need to use new keyword to instantiate the object.


    Interested readings on OOP:

    • Introduction to Object-Oriented JavaScript
    • Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications and libraries by Stoyan Stefanov
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a simple question from a relative Java newbie: what is the difference between
I am a relative newbie to Hadoop MapReduce. I was trying a variation of
I'm a relative newbie on iPhone app development but have successfully created a tab
Javascript newbie here, just trying to tweak this bit of code. It checks for
I'm a relative newbie to thinking in OOP terms, and haven't yet found my
As a relative newbie I try to read as much as I can about
I am a relative newbie when it comes to SQL Azure!! I was wondering
Can anyone explain the following behaviour to a relative newbie... const char cInputFilenameAndPath[] =
I am a relative newbie to Rails and Mongoid so apologies if the answer
I'm a relative newbie to web development. I know my HTML and CSS, 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.