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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:33:13+00:00 2026-06-04T14:33:13+00:00

I have been using Javascript but more in a procedural way not the object

  • 0

I have been using Javascript but more in a procedural way not the object oriented way. Recently, I am just getting started to learn Javascript in a OOP manner. Can anybody tell what is the difference between:

var foo = {
   method1 : function(){
  }
}

and

var foo = function(){
   method: function(){
  }
}

Are both of these same or is the 1st one static class? If both are same then my 2nd question is how do I write methods and call it statically?

  • 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-04T14:33:14+00:00Added an answer on June 4, 2026 at 2:33 pm

    In the first case, you define an object using an object literal. From the view of OOP, it is a singleton.

    var myObject = {
        field: value,
        method: function() { }
    }
    

    The other one is syntactically incorrect. What that code probably wanted to resemble, however, is a constructor.

    var myConstructor = function() {
        this.field = value;
        this.method = function() {};
    }
    

    If this myConstructor is called with the new keyword,

    • a new object will be created
    • myConstructor will run with this set to reference this new object, so the field and method properties will get appended to it
    • the new object will be returned

    Example:

    var myObject = new myConstructor();
    

    Every new instance of object constructed by myConstructor() will receive its own copy of the function method. This is way prototype is usually used to store functions, but that is out of the scope of this question.

    As for the static methods, a possibility is to add it directly to the constructor. This is possible, because functions are objects too in JS. Imagine this example.

    var myConstructor = function() {
       // keep track of this instance
       myConstructor.addInstance(this);
    }
    
    myConstructor.instances = new Array();
    
    myConstructor.addInstance = function(obj) {
        myConstructor.instances.push(obj);
    }
    
    myConstructor.getInstances = function() {
        return myConstructor.instances;
    }
    
    var myObject1 = new myConstructor();
    var myObject2 = new myConstructor();
    alert(myConstructor.getInstances()); // [object Object],[object Object]
    

    In this case, the constructor contains an array of all existing instances and methods to manipulate it. These fields and methods may be considered static properties of myConstructor, because they are included in the constructor (which substitutes classes of the classical OOP) itself and no instantiation is needed to access them.

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

Sidebar

Related Questions

I have been using JavaScript as a tool for many years now, but only
I've been using javascript for a while, but have never learned the language past
What is everyone's favorite way to sanitize user data? I've been using Javascript, but
We have been using javascript hashes a lot lately, and we've been looking for
Hi I have been trying to learn Javascript using codeacademy.com and I have reached
I've been using Firebug to debug some javascript I have on one of my
I have been using the CodeIgniter system for a while now - but it
I have been using telerik extensions for an asp.net mvc project, more specifically the
I have been using jQuery lately, but never used JSON with it. Now, I
I have been a desktop developer for a few years mostly doing object oriented

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.