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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:32:26+00:00 2026-06-05T10:32:26+00:00

I want to add the ability to extend javascript objects by adding a method

  • 0

I want to add the ability to extend javascript objects by adding a method to the prototype.
The method will receive one or more other objects and will add all of the key/values to this.
This is what I came up with:

Object::extend = (objects...) ->
    @[key] = value for key, value of object for object in objects

or this:

Object::extend = (objects...) ->
    for object in objects
        for key, value of object
            @[key] = value 

Both work as expected, and compile into the same javascript code:

var __slice = [].slice;

Object.prototype.extend = function() {
  var key, object, objects, value, _i, _len, _results;
  objects = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  _results = [];
  for (_i = 0, _len = objects.length; _i < _len; _i++) {
    object = objects[_i];
    _results.push((function() {
      var _results1;
      _results1 = [];
      for (key in object) {
        value = object[key];
        _results1.push(this[key] = value);
      }
      return _results1;
    }).call(this));
  }
  return _results;
};

What I’m not too happy about is the whole results thing that is created per for loop which is completely redundant for my purpose.
Is there a way to get a code more like:

Object.prototype.extend = function() {
  var key, object, objects, value, _i, _len;
  objects = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  for (_i = 0, _len = objects.length; _i < _len; _i++) {
    object = objects[_i];
    (function() {
      for (key in object) {
        value = object[key];
        this[key] = value;
      }
    }).call(this);
  }
};

Thanks.


Edit

I’m aware that I can simply embed javascript code, but looking for a coffeescript solution.

  • 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-05T10:32:28+00:00Added an answer on June 5, 2026 at 10:32 am

    You can try adding an explicit return:

    Object::extend = (objects...) ->
        for object in objects
            for key, value of object
                @[key] = value
        return 
    

    That produces this:

    var __slice = [].slice;
    
    Object.prototype.extend = function() {
      var key, object, objects, value, _i, _len;
      objects = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      for (_i = 0, _len = objects.length; _i < _len; _i++) {
        object = objects[_i];
        for (key in object) {
          value = object[key];
          this[key] = value;
        }
      }
    };
    

    Every CoffeeScript function returns the value of the last expression in the function, CoffeeScript loops are also expressions. That means that CoffeeScript has to build all that _results stuff to produce a return value for your function since you had an implicit return applying to the outer loop. If you remove that implicit return by adding an explicit “return nothing” then the CS→JS compiler seems to be smart enough to not do all that extra _results work.

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

Sidebar

Related Questions

In one of my Facebook Apps, I want to add the ability for the
I want to extend or copy the PopUpManager class to add the ability to
I want add 12 UITextFields in a UITableViewCell (one text field in each row),
I want add my custom sidebar next right column all page. Please check this
I write a module,and want add it to kernel.It will print a world when
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
I am building a text chat system. I want to add the ability to
I'm writing an LCD controller, and one feature I want to add is the
Im writing WPF application and want to add ability to call jump list and
I want to add the ability in my application which is using Spring framework

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.