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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:13:12+00:00 2026-05-29T11:13:12+00:00

What’s the proper way to create an object (with its namespaces and such)? 1

  • 0

What’s the proper way to create an object (with its “namespaces” and such)?

1

//Company object
var Microsoft = {};

//Create an employee
Microsoft.employee = function(name) {
   this.name = name;
}

or

2

//Company object
Apple = {

   employee: function(name) {
      this.name = name;
   }

}

OR another way? Shoot.

Read something about prototypes and such. What’s the proper way to do it; benefits and downsides?

  • 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-29T11:13:13+00:00Added an answer on May 29, 2026 at 11:13 am

    First off, you forgot the var for Apple. But otherwise these are basically the same thing.

    Secondly, in my examples I’m not going to use the attribute name since, when dealing with functions, the name is an empty string by default. At least in Node.js and Chrome. So I’ll use empName instead.

    In the Microsoft example you are making an empty object and then adding an attribute to it after the fact.

    In the Apple example you are making an object with the attribute right away.

    It’s really just what makes the most sense to you, and which you prefer. Since they are, more or less, equivalent.

    Now, this has nothing to do with prototypes. Here’s an example of what you did:

    var Apple = { 
        employee: function(empName) {
            this.empName = empName;
        }
    };
    
    Apple.employee('Hank');
    Apple.empName; // 'Hank'
    

    And here’s how you would do this with an instance (using the new operator, and the prototype)

    var Apple = function() {}; // base 'parent'
    
    Apple.prototype.employee = function(empName) {
        this.empName = empName
    };
    
    var a = new Apple();
    a.employee('Hank');
    a.empName; // 'Hank'
    Apple.empName; // undefined
    

    So prototype is used to add attributes to new instances of an object (using ‘object’ loosely). Note that to access employee in Apple, on this second example, you would have to do something like

    Apple.prototype.employee('Hank'); // doesn't really do much
    Apple.empName; // undefined
    
    // but you can call the employee prototype with a bound variable
    // you'd do this if you don't want to make an instance of Apple
    // but still want to use one of it's prototypes
    var obj = {};
    Apple.prototype.employee.call(obj, 'Hank');
    obj.empName; // 'Hank'
    
    // a practical use of accessing a prototype method is
    // when wanting to convert a function's arguments
    // to an array. function arguments are like an array,
    // but until turned into one they are not completely the same
    var func = function() {
        var args = Array.prototype.slice.call(arguments);
        var sum = 0;
        for(var i = 0, l = args.length; i < l; i++) {
            sum += args[i];
        }
        return sum;
    };
    
    func(1); // 1
    func(1, 2, 3, 4, 5); // 15
    

    Hope that helps.

    EDIT: Also, don’t prototype objects (e.g. {} or Object). It’s not safe to do this. Since, essentially, every variable in JavaScript is an object, then any prototypes you add to them will be available on all variables. So if you did Object.prototype.xyz = 12 then had var obj = { a: 1, b: 2, c: 3} and then tried for(var key in obj) { console.log(key); } you would result in the following logs: a, b, c and xyz … which you wouldn’t want.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string

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.