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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:12:10+00:00 2026-06-09T03:12:10+00:00

Update : I know there are existing implementations of forEach method. The purpose of

  • 0

Update: I know there are existing implementations of forEach method. The purpose of doing this is to learn and improve my Javascript skills.

I am trying to implement a forEach method for array objects. What I am doing is the following:

var list = ['one', 'two', 'three'];

function forEach(callback){
    for(var n = 0; n < this.length; n++){
        callback.call(this[n], n);
    }
};

list.forEach(function(index){
        console.log(index);
        console.log(this);
    }
);

I am not very good with javascript and I am trying to get better so I’ve been reading a little bit and I now know that if I do this kind of thing, the context of the “forEach” function would be the object which is calling it, in this case “list”.

When this code runs I get the error: “Uncaught TypeError: Object one,two,three has no method ‘forEach'”.

What is it I am not understanding?

Thanks!

  • 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-09T03:12:12+00:00Added an answer on June 9, 2026 at 3:12 am

    I think what you want to do is probably not to attach the foreach function to only the one array you have here, but to make it work for all arrays. To do that, you must edit the Array prototype (something that some people have very strong opinions about, because you can not protect against potential future namespace collisions – but other people find extremely useful). Once the Array.prototype object has a function added to it, you can call that function on any array. Like this:

    var list = ['one', 'two', 'three'];
    
    Array.prototype.myForEach = function(callback){
        for(var n = 0; n < this.length; n++){
            callback.call(this[n], n);
        }
    };
    
    list.myForEach(function(index){
        console.log(index);
        console.log(this);
    });
    

    *N.B. to avoid conflict with existing forEach functions (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach) I have named the function myForEach which I expect to be safe from conflict.

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

Sidebar

Related Questions

I want to know if there's any way to update from all versions (Example:
I want to know Id update panel that initial a request in JavaScript .I
I know that in standard SQL you can do this: update top (100) table1
I know there are similar questions on this subject but nothing that quite answers
I am developing a SSIS package, trying to update an existing SQL table from
I know there is a command that updates the changes like c:\svn up <working
UPDATE: I know I can use <ol> directky in the output but I remember
Anyone know how to do an update with a join (i.e. update on two
I know the question of how to update multiple tables in SQL has been
Does anyone know what the esiest way to update the entity model after adding/deleting

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.