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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:31:24+00:00 2026-05-20T22:31:24+00:00

I saw this code before, but I don’t know what the meaning: var person1

  • 0

I saw this code before, but I don’t know what the meaning:

var person1 = {
    toLocaleString : function(){
      return "Nikolaos";
     },
    toString : function(){
      return "Nicholas";
    }
}

var person2 = {
   toLocaleString : function(){
      return "bum";
   },
   toString : function(){
       return "Greg";
   } 

}

var people = [person1, person2];
alert(people.toString());
alert(people.toLocaleString());

does the function create an object with the method of toLocaleString and toString??or…??

  • 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-20T22:31:25+00:00Added an answer on May 20, 2026 at 10:31 pm

    That code is doing three things:

    1. Using object literal syntax to create object instances
    2. Using anonymous function expressions to create functions and bind them to properties on the objects. (Functions are first-class objects in JavaScript, so you can keep references to them, pass the references around, etc.)
    3. Specifically, it’s overriding two standard functions that all JavaScript objects inherit from the Object prototype.

    Let’s break it down a bit.

    1) Object literal notation:

    var obj = {propName: propValue};
    

    The { and } in this case denote an object literal. Within an object literal, you can write propName: propValue to assign propValue to the property with the name propName on the object. This is the same as:

    var obj = {};             // Get an empty object
    obj.propName = propValue; // Add a property to it
    

    You can do multiple properties separated with commas. So for instance:

    var obj = {
        author: "Douglas Adams",
        title:  "The Hitchhiker's Guide to the Galaxy",
        answer: 42
    };
    

    That creates an object with three properties, two with string values and one with a number value.

    Note that the right-hand side are processed just like an assignment, and so can be anything that can appear on the right-hand side of an assignment statement:

    var x = "bar";
    var obj = {
        three: 1 + 2,
        fubar: "foo " + x
    };
    

    The property names can be put in quotes if you like:

    var x = "bar";
    var obj = {
        "three": 1 + 2,
        "fubar": "foo " + x
    };
    

    …which is handy for specifying properties that have the names of reserved tokens (like “if”, or “return”) or formerly-reserved tokens (like “class”) where it would be a syntax error if they weren’t in quotes.

    2) Now let’s look at function expressions:

    var f = function() { /* your code here */ };
    

    That’s a function expression. It creates a new function and assigns a reference to it to the variable f. You can call it by calling f().

    var f = function(name) {
        alert("Hi " + name);
    };
    f("Fred"); // alerts "Hi Fred"
    

    1 + 2) So putting it together with object literal notation:

    var obj = {
        foo: function(name) {
            alert("Hi " + name);
        }
    };
    obj.foo("Fred"); // alerts "Hi Fred"
    

    (I don’t like anonymous functions, I prefer my functions to have names, but that’s another topic.)

    3) And finally: As maerics pointed out, the specific functions that are being used in that code are toString and toLocaleString, both of which are standard functions of JavaScript objects. That means that those will override the standard version and so return the given values whenever the standard function would have been called.

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

Sidebar

Related Questions

Before you mark this as answered in another post (I already saw those). But
I know that this has been asked a million times before, but nothing that
I saw this code snippet during our lab and it actually compiles in MSVC2008
I just saw this code while studying the wordpress source code (PHP), You can
Saw this piece of code in a Ruby on Rails book. This first one
I saw this bit of code on the interents somewhere. I'm wondering what the
const static int foo = 42; I saw this in some code here on
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
I saw some code like this: try { db.store(mydata); } finally { db.cleanup(); }
I saw code similar to this in the Mac OS SDK: enum { kAudioFileStreamProperty_ReadyToProducePackets

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.