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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:05:25+00:00 2026-05-26T05:05:25+00:00

I want to write a function like jQuery each and am using the javascript

  • 0

I want to write a function like jQuery each and am using the javascript call function.

function each(array, callback) {
    for (var i=0; i<array.length; i++) {
        console.log(typeof(array[i])); // Number
        callback.call(array[i]);
    }
}

each([1,2,3], function() {
    console.log(typeof(this)); // Object
});

The problem is call seems to be casting Number types to Object types. This causes issues with the console.log call. Can anyone explain why this is happening (my guess is that call casts arguments to type Object). Why would it do this? Can you figure out a way to work around or prevent this?

  • 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-26T05:05:26+00:00Added an answer on May 26, 2026 at 5:05 am

    This is a requirement of the specification when not in strict mode.

    If you use the "use strict"; declarative at the top of your code, you’ll get whatever actual value you passed.

    DEMO: http://jsfiddle.net/agXkZ/

    "use strict";
    function each(array, callback) {
        for (var i=0; i<array.length; i++) {
            callback.call(array[i]);
        }
    }
    
    each([1,2,3], function() {
        console.log(this);
    });
    

    Note that strict mode is lexically scoped, so you could add the declarative to the callback only if you’d prefer.

    DEMO: http://jsfiddle.net/agXkZ/1/

    function each(array, callback) {
        for (var i=0; i<array.length; i++) {
            callback.call(array[i]);
        }
    }
    
    each([1,2,3], function() {
        "use strict";
        console.log(this);
    });
    

    If you don’t want to use strict mode, you can (in this case) convert to a primitive by using the unary + operator.

    DEMO: http://jsfiddle.net/agXkZ/2/

    function each(array, callback) {
        for (var i=0; i<array.length; i++) {
            callback.call(array[i]);
        }
    }
    
    each([1,2,3], function() {
        console.log( +this ); // <--converts from object to primitive
    });
    

    Related information:

    From ECMAScript 5 Annex E (informative) Additions and Changes in the 5th Edition that Introduce Incompatibilities with the 3rd Edition:

    15.3.4.3, 15.3.4.4: In Edition 3 passing undefined or null as the first argument to either Function.prototype.apply or Function.prototype.call causes the global object to be passed to the indirectly invoked target function as the this value. If the first argument is a primitive value the result of calling ToObject on the primitive value is passed as the this value. In Edition 5, these transformations are not performed and the actual first argument value is passed as the this value…

    And ECMAScript 5 Annex C (informative) The Strict Mode of ECMAScript:

    If this is evaluated within strict mode code, then the this value is not coerced to an object. A this value of null or undefined is not converted to the global object and primitive values are not converted to wrapper objects. The this value passed via a function call (including calls made using Function.prototype.apply and Function.prototype.call) do not coerce the passed this value to an object.

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

Sidebar

Related Questions

In jQuery you can do : $(a[href$='.img']).each(function(index) { alert($(this).attr('href')); } I want to write
I want to write a function like equalp, which gives #t for (equalp Xy
I want to write a cmp -like function that compares two version numbers and
I want to write a generic function to calculate factorial in C# ... like:
I want to write a function that takes an array of letters as an
if i extend the jquery fn (like $.fn.extend) i write my plugin: (function($){ $.fn.extend({
I am using the JQuery.Sortable() function to create a reorderable list of items like
I'm writing a psychology experiment in jQuery. I want to write a function that
I want to use a JQuery check all function in a form like this:
Hi I've tried to write JavaScript (jQuery) function to merge values from input type

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.