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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:44:01+00:00 2026-06-07T12:44:01+00:00

In javascript what’s the best way of declaring objects? This one var Class =

  • 0

In javascript what’s the best way of declaring objects?

This one

var Class = function(){};

Class.prototype.print = function(){
    console.log('hello world');
};

or this one

var Class_2 = {};

Class_2.print = function(){
    console.log('hello world');
};

in which case I should use one method over another?

  • 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-07T12:44:03+00:00Added an answer on June 7, 2026 at 12:44 pm

    It depends if you want to create a class or a single object.

    JavaScript OOP model is based on prototypes, not on classes. That’s why you can add properties and methods not only to a class (meaning that all “instances” of that class will have that properties or method), but also to a particular object.
    So you could have object A and B instances of class C, and you could add special behavior to A that only A has, and B doesn’t has it…
    All this sounds strange, because JavaScript doesn’t actually have classes. It only has prototypes, and you can more or less “simulate” classes by using the things you can do with prototypes.
    In JavaScript when an object A is an “instance” of an other object B, you are actually saying that B is the prototype of A. That means that you’ll define A based on the differences it has on B.

    In real world that would mean that your first ever encounter with a Sheep, was with Dolly, a big, white, hairy sheep. So you say Dolly.color = white; Dolly.hair = a_lot; Dolly.size = big; Then somebody asks you what’s the color of a Sheep, and since you only know Dolly, you think that all Sheeps are exactly like dolly and you answer white… Then you met another Sheep, Pete, but this one is black and big and hary. So you say that Pete is exactly like Dolly, except that Pete is black….
    So you’ll say that Pete.Prototype = Dolly ( That means that is exactly like dolly), but then you’ll overwrite the color and say it’s different: Pete.color = black;

    To “simulate” Classes in JavaScript what you do is define an “Ideal” Sheep called Sheep that posses all the “default” qualities of all sheeps, and use that as a prototype for all the other sheeps.

    There are many ways to define classes in JavaScript. But you should always remember that you aren’t actually defining classes, but prototypes, and simulating classes. You’d better try learning about prototypes, if you really want to understand what is going on, and how to get the best out of it.

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

Sidebar

Related Questions

Javascript functions can be declared on a objects prototype like this: <object name>.prototype.<variable name>=function(){
Javascript Code var d = function c() { console.log(c); }; d(); // function c()
JavaScript/JQuery var arr=[]; $('.element').each(function(i) { arr.push({id:i,value:$(this).attr('data-value')}); }); $.get('/json.php?arr='+arr,function(result) { alert(result); }); PHP <?php $j
JavaScript bit: $(document).ready(function() { $('#form').submit(function(e) { e.preventDefault(); var $form = $(this); // check if
JavaScript does funky automatic conversions with objects: var o = {toString: function() {return 40;
javascript code: function getCheckbox(name,id){ var check = 0; var deger = option-checkbox-+id; var dom
JavaScript: $('.addTable').live('click', function () { var appendTxt = <table id='tab1'> <tr><td><input type='text' name='input1' /></td><td><input
Javascript jQuery event handling If on event (for example 'click') bound one function for
Javascript: var images = $('#slideshow').children(); var dot_holder = $('#slider_dots'); /* Create the dots*/ images.each(function(index,item){
Javascript passes objects by reference. This makes perfect sense. But once you start manipulating

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.