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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:45:14+00:00 2026-05-26T18:45:14+00:00

I’m in the process of writing a simple prototype in Node.js with some helper

  • 0

I’m in the process of writing a simple prototype in Node.js with some helper methods that I’ll probably need in objects using that prototype. One method that I’d like to have is an implementation of jQuery’s .each(). I’ve looked at jQuery’s implementation in their development release, and tried to emulate it in my simplified version here.

// Loop through the object using a callback
BaseProto.prototype.each = function (cb, args) {
    var obj = this.get(),   // Get our object as a plain object
        prop;

    /** Code to make sure the the args passed are actually an array **/

    if (typeof cb === "function") {
        // For each property in our object
        for (prop in obj) {
            // Apply the callback to the object's property and pass
            // the index, the property, and whatever else as arguments
            if (cb.apply(obj[prop], [ prop, obj[prop] ].concat(args)) === false) {
                // Get out if the function returns false
                break;
            }
        }
    }

    // Reset our object with the new one
    return this.reset(obj);
};

The problem is that while the callback is definitely being fired, it doesn’t have any effect on the object’s property. No matter what I do inside the callback, the changes stay inside the callback’s scope.

Here is an example of a simple callback that I’ve been testing with.

var BaseProtoTestObj = new BaseProto();

/** Set some properties to BaseProtoTestObj **/

function cb1 ( key, val ) {
    var prop;

    key = key.toString() + " Callbacked";
    val = val.toString() + " Callbacked";

    for (prop in this) {
        this[prop] = this[prop].toString() + " Callbacked";
    }
}

// Doesn't have any effect on BaseProtoTestObj
BaseProtoTestObj.each(cb1);

I can see that there is a lot more going on in jQuery’s .each(), but from what I can gather it’s for optimization and the ability to iterate over arrays as well as objects.

In the end, my question is simple. What is it that jQuery is doing to affect the properties, that I’m not in my .each()?


Edit

I suppose another question to ask would be if my logic is fundamentally wrong, and you can’t modify an object’s properties this way.

  • 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-26T18:45:15+00:00Added an answer on May 26, 2026 at 6:45 pm

    You don’t need a custom method:

    for(var prop in object) {
       var value = object[prop];
       // do something with value and/or prop
    }
    

    Although if you really needed .each(), you could do something like this:

    Object.prototype.each = function(cb) {
        for(var propName in this) {
            cb(propName, this[propName]);
        }
    }
    
    var foo = { prop: 'value', prop2: 'value2' };
    foo.each(function(key,value) {
        // do something here
    });    
    

    Since you need to modify the actual values of the properties, try this:

    Object.prototype.mutate = function(cb) {
        for(var propName in this) {
            this[propName] = cb(propName, this[propName]);
        }
    }
    
    var obj = {
        a: 'foo',
        b: 'bar',
        c: 'baz'
    };
    
    obj.mutate(function(propName, propValue) {
        return propName + '-' + propValue;
    });
    
    /*
    obj will now be:
    
    var obj = {
        a: 'a-foo',
        b: 'b-bar',
        c: 'c-baz'
    };
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am reading a book about Javascript and jQuery and using one of the
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
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.