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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:32:36+00:00 2026-05-28T18:32:36+00:00

I have implemented each function like this in JS: Object.prototype.each = function(fn){ for (var

  • 0

I have implemented each function like this in JS:

Object.prototype.each = function(fn){
  for (var x in this){
    if (this.hasOwnProperty(x)){
        fn(x);
    }
  }
  return this;
};


var o = {'a' : 'apple', 'b' : 'bat', 'c' : 'cat'};
o.each(function(i){
   alert(i);
});

Though this works fine. Is there anything wrong in it. Asking because I am learning JS.

Also why does it produces error if I do:

{'a' : 'apple', 'b' : 'bat', 'c' : 'cat'}.each(function(i){
   alert(i);
});
  • 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-28T18:32:38+00:00Added an answer on May 28, 2026 at 6:32 pm

    You need to put parentheses around the object or it will interpret it as a block due to the opening and closing curly brackets.

    ({'a' : 'apple', 'b' : 'bat', 'c' : 'cat'}).each(function(i){
       alert(i);
    });
    

    When you do it without the parentheses, the interpreter will “see” this:

    { // open block statement
       'a' : 'apple', 'b' : 'bat', 'c' : 'cat' // not a valid expression!
    }.each(function(i){ // there's no dot property access notation for statements!
       alert(i);
    });
    

    Which is a block with a single expression. However, since that is not a valid expression it will return a syntax error. Even if it were a valid expression, it would still give you an error since blocks do not have properties and therefore the dot property access notation is not applicable:

    { console.log("hello world"); }.each(function(){})
    

    Thus, you need to tell the interpreter that the object is an expression by wrapping it in parentheses to remove the ambiguity (since you cannot put a statement inside a parentheses – only expressions).

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

Sidebar

Related Questions

I have an object that has a mathematical function behind it. It seems like
I have an object that looks like this: [Bindable] public class MyRecord implements ValueObject
I have implemented a python webserver. Each http request spawns a new thread. I
I have implemented inheritance for two parent classes called Table and Field . Each
I have this Two Methods private function cacheAdd($id,$data) { $this->minicache->set($id,$data); } private function cacheGet($id)
I have a set of classes something like this: abstract class CollectionAbs implements Iterator
I have an object oriented framework that uses a page design, where each page
I would like to make a query from an object I have defined that
I have a webpage that implements a set of tabs each showing different content.
I have implemented what I thought was a pretty decent representation of MVC in

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.