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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:00:40+00:00 2026-05-24T08:00:40+00:00

As a c#/.net dev, I love to toy around with JavaScript in my spare

  • 0

As a c#/.net dev, I love to toy around with JavaScript in my spare time — creating my own libraries/frameworks and such. Admittedly, they’re not much (really nothing more than a loose collections of functions), but the purpose is to learn; not for other people to use.

I usually extend a basic JavaScript object this way

obj = function () {
    //basic object stuff
    this.method = function () {
        //other stuff
    };
    return this;
};

This allows me to create other objects and chain methods together, which is really slick:

obj('arg1').method();

Two Examples: jQuery Knock-off, List-Item Sorter

However, I have recently seen, in much more function code than my own, objects accomplish the same functionality this way:

function obj(){
    //stuff
}
obj.prototype.method = function () {
    //stuff
};

Example: Reddit Chrome Extension

Both ways seem to accomplish the same end, and I’m not partial to the look of either syntax. Is there a particular situation where one would be more useful than the other? What do these methods offer that makes them more desirable than the other?

Edit

Consider the following code:

var dice = function (sides) {
    this.roll(){
        return 4 //guaranteed to be random
    }
};

var d1 = dice(6);
d1.roll()  // 4;
var d2 = dice(20);
d2.roll()  // 4

Are d1 and d2 different objects, as they appear to me? Or are they pointers/nicknames to one object (var dice)?

  • 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-05-24T08:00:41+00:00Added an answer on May 24, 2026 at 8:00 am
    this.method = function(){};
    

    Only works for that specific instance.

    Obj.prototype.method = function(){};
    

    Will work for every instance of Obj

    Though in order to take advantage of prototype you should do

    var o = new Obj(); // Note that functions intended to be used with "new" should be capitalized
    
    o.method();
    

    Dice Example

    I’ll assume you intended to return this in your dice() function.

    That example is not really common, because calling a function does not create a new object. In your dice case you would be assigning a method to this, which inside the function is window the global object, and then returning it.

    The outcome would be the same object (window) in both d1 and d2, with a method roll which would be reassigned in the 2nd call.

    To achieve what you want you should create the instances with new, like this:

    var d1 = new Dice(6); // remember capitalization is important here
    var d2 = new Dice(20); 
    

    This will however create 2 roll functions, which is correct but wastes memory since the function can be shared by doing:

    Dice.prototype.roll = function() { /* return random awesomeness */ };
    

    Hope that clarifies things

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

Sidebar

Related Questions

I'm trying to play around with the Farbtastic: http://acko.net/dev/farbtastic color picker plugin but I'm
basically, here is the address... http://opentools.homeip.net/dev-tools-for-upnp they are the recommended dll's to use for
I'm currently trying to get my head around WCF services for an ASP.NET dev
I am a .NET dev learning Java. As a result, I understand all the
The output of /proc/net/dev on Linux looks like this: Inter-| Receive | Transmit face
Is there any way to access my asp.net dev server (launched from visual studio)
I am a total total beginner in Java actually I am .net dev trying
A number of team members update a central ASP.NET dev Website project, not a
I'm migrating a website made in classic asp to asp.net, but the asp.net dev
Does something like OpenSSO ( https://opensso.dev.java.net/ ) or ESOE ( http://esoeproject.org/ ) exist for

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.