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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:29:17+00:00 2026-06-13T16:29:17+00:00

I just need to know what is the rudimentary difference between creating an object

  • 0

I just need to know what is the rudimentary difference between creating an object using these two methods, and the effect of using the objects.

//////////////////////////////////////////
myTestObject.prototype.var1 = null;
function myTestObject()
{
    this.var1 = "test me";
}

myTestObject.prototype.action = function()
{
    alert("alertme");
};
// to use
var a = new myTestObject();
a.action();
//////////////////////////////////////////


//////////////////////////////////////////
(function()
{
    $.myTestObject3 = {
      var1 : "test me",
      action : _actionMethod
    };

    function _actionMethod()
    {
        alert("alertme3");
    }

 })();


$.myTestObject3.action();
//////////////////////////////////////////

A few updates… the answers to this question lead me to google for different keys words which lead to two interesting articles which really went into a lot of detail about Constructor vs Literal objects in javascript

http://net.tutsplus.com/tutorials/javascript-ajax/the-basics-of-object-oriented-javascript/

Then as a segue into why to use prototype when using function constructors paying particular attention to the section on Why is Using Prototype Better

http://net.tutsplus.com/tutorials/javascript-ajax/prototypes-in-javascript-what-you-need-to-know/?search_index=1

So as a follow up, you can use a combination of the two like in the guitar example below as a good practice. If only one copy of an object is needed and a change to it, knowing it affects the whole script is ok, then an object literal is fine. However if many objects need be created using the prototype approach is best so all created objects use a reference to the same function rather than having a copy of the function.

Another good article to make better use of namespacing and combining all these approaches
http://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/

LAST FOLLOW-UP
I hope others find this useful as these topics threw me for a loop for a while.

Knowing what to look for now, I found two really good urls that describe many design patterns and how to intermix them in javascript using several examples.

This one describes how to use the module pattern in a way that makes use of the prototype notation. This will allow you to compartmentalize code while still making sure that instances are referencing the same objects.
http://briancray.com/posts/javascript-module-pattern/

This goes through several design patterns and list advatanges and disadvantages
http://addyosmani.com/resources/essentialjsdesignpatterns/book/

  • 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-13T16:29:18+00:00Added an answer on June 13, 2026 at 4:29 pm

    Take a look at Why is JavaScript prototyping? , some extract from there:

    If you add all of your properties and methods to the object function
    constructor, then create 100 instances of that object, you get 100
    copies of all of the properties and methods. Instead, if you add all
    of your properties and methods to the prototype of the object function
    constructor, then create 100 instances of that object, you get 100
    references to the single (1) copy of the object’s properties and
    methods. This is obviously faster and more efficient and is why
    prototype is used (aside from altering things like String and Image,
    as mentioned below)

    Another reason to use prototypes is to emulate classical inheritance:

    var Instrument = {
        play: function (chord) {
          alert('Playing chord: ' + chord);
        }
    };
    
    var Guitar = (function() {
        var constructor = function(color, strings) {
            this.color = color;
            this.strings = strings;
        };
        constructor.prototype = Instrument;
        return constructor;
    }());
    
    var myGuitar = new Guitar('Black', ['D', 'A', 'D', 'F', 'A', 'E']);
    myGuitar.play('D5');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just need to know the difference between two int variables a and b
I'm creating a site using asp.net and C# and i just need to know
I just need to know the owner's name of a file on Mac using
I really just need to know if this is possible without writing two queries
I just started using libxml to parse the xml. I just need to know
I just need to know if WCF is platform independent like Webservices? Can the
I just need to know hot to create a CGLayer that has an image
I have solved this problem, I just need to know what to do. I
I'm making an app and I'm almost done. I just need to know how
NOTE: I am aware of the SVN relocate switch, I just need to know

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.