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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:11:36+00:00 2026-06-07T03:11:36+00:00

Consider the below code. I created an empty object. And am adding var1,var2,var3 without

  • 0

Consider the below code.

I created an empty object. And am adding var1,var2,var3 without declaring them.

var har = new Object();
har.var1 = "Var1";
har.var2 = "Var1";
har.var3 = "Var1";

alert( har.var1 );

If I can do this, then why do I need to create a Class and fix the attributes when I can introduce new attributes anytime?

  • 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-07T03:11:37+00:00Added an answer on June 7, 2026 at 3:11 am

    Why would you even need to use objects in the first place? Non-object-oriented languages are already Turing-complete, so everything you can accomplish with objects you can also do without them.

    What you’re showing in your example is not really an object, but just a dictionary. In cases like this, where you only need to keep several related properties together, anonymous unprototyped objects like the one you’re using are the de-facto standard approach (though it is customary to initialize them with the shorthand syntax, e.g. var har = {}). It is an object, since it uses the object data structure, but it is not object-oriented.

    Actual objects, in contrast, not only define data, but also the operations that you can perform on that data. Objects have not only properties, but also methods which work on these properties. These properties are usually defined in the object prototype (which you’re calling “class”, but Javascript is not a class-based language, but a prototype-based one). All methods defined in the prototype are shared between all instances of that prototype.

    function Counter() {
        this.counter = 0;
    }
    
    Counter.prototype.increment = function() {
        this.counter++;
        alert(this.counter);
    }
    
    var c1 = new Counter();
    var c2 = new Counter();
    
    c1.increment(); // alerts 1
    c1.increment(); // alerts 2
    c2.increment(); // independent from c1: alerts 1 again
    

    Each instance is still a dictionary, as in your example (and you can even still add more properties to them, they are not “fixed” by having a constructor and prototype), but now it can also perform tasks on its properties. This can be done your way as well:

    c1 = {
        counter: 0,
        increment: function() {
            this.counter++;
            alert(this.counter);
        }
    }
    
    c2 = {
        counter: 0,
        increment: function() {
            this.counter++;
            alert(this.counter);
        }
    }
    

    You can see, however, that if you need two counters, without using prototypes you will need to duplicate your entire object definition. This will be cumbersome to write and maintain, and each increment function will be defined separately, thus it will also waste memory.

    That said, in cases where you need an object that you know you’ll only ever need one instance of, it makes no sense to define a constructor and a prototype for it. Such objects are usually regarded as namespaces instead of actual objects.

    Appendix: Dictionaries vs Objects

    A dictionary is a data structure which holds named elements. Besides “dictionary”, they are also called associative arrays or hashmaps. Objects in Javascript are implemented as dictionaries — each property is a named element in the dictionary. In addition to a plain dictionary, objects also have a prototype, which is kind-of like a parent object — when you look up a named element in the dictionary and it is not there, it is automatically searched for in the prototype as well. This way, default properties and methods are defined only once in the prototype, and do not need to be copied into each instance. (The prototype of an object is often visible as the instance.__proto__ property, though this is non-standard and deprecated even in browsers that support it; the actual prototype is defined as an internal, non-accessible property by the standard)

    So, Javascript objects are actually dictionaries. When you want to use a plain dictionary to store some related properties together, there is no separate syntax in Javascript to create a dictionary that is not an object, so you create an instance of the base Object type to hold your dictionary (it does not matter if you do var dict = new Object or var dict = {}, the result is the same); thus, dictionaries that you use in your code are also objects.

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

Sidebar

Related Questions

Consider the code below, private void Convert_Click(Object sender, RoutedEventArgs e) { string[] strCmdLineParams =
Consider the code below. I don't understand why my GCC compiler does not try
Consider the JavaScript code below, inspired from the YUI documentation on YAHOO.lang.extend . In
Consider the Java code below, what would happen if there were no paintComponent method
I am having trouble finding an answer to this. Consider the clipping code below:
I have a code snippet as below <CheckBox Name=cb Margin=1,2,1,0 IsChecked={Binding Path=IsManager} IsEnabled=True/> Consider
My questions are divided into three parts Question 1 Consider the below code, #include
Consider the below C++ code class B; class A{ private: B* mB; }; class
consider the code below: #include list.h struct List { int size; int* data; };
Consider the code below (which has been simplified). I have a service class that

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.