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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:13:11+00:00 2026-06-17T14:13:11+00:00

I have some code like this: function Foo( arr, prop ) { this.arr =

  • 0

I have some code like this:

function Foo( arr, prop ) {
  this.arr = arr;
  this.isOn = prop;
}

function newFoo( arr, prop ) {
  return new Foo( arr, prop );
}

Foo.prototype = {

  a: function() {
    var result = [];
    // do something and push to result
    if ( this.prop ) // do something different with result
    return newFoo( result );
  },

  // This is the method that determines if prop = true in the chain
  b: function() {
    result = [];
    // do something and push to result
    // this time 'prop' must be 'true'
    return newFoo( result, true )
  }

};

I want to keep passing true if the previous element in the chain has prop. Obvisouly the above approach doesn’t work as you can see here:

var nf = newFoo;
console.log( nf( [1,2,3] ).b().isOn ); //=> true
console.log( nf( [1,2,3] ).b().a().isOn ); //=> undefined

I know I could just return newFoo( result, this.prop ) all the time on every method but I was curious to see if there are any other solutions to this problem. As methods grow in number it’ll be hard to keep track of this property over time.

  • 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-17T14:13:12+00:00Added an answer on June 17, 2026 at 2:13 pm

    As methods grow in number it’ll be hard to keep track of this property over time.

    You could just create an extra method with the functionality of newFoo that automatically keeps track of the properties you are not going to overwrite:

    function Foo( arr, prop ) {
      this.arr = arr;
      this.isOn = prop;
    }
    
    Foo.prototype = {
    
      clone: function newFoo( arr, prop ) {
        return new Foo(
          arguments.length >= 1 ? arr : this.arr,
          arguments.length >= 2 ? prop : this.isOn
        );
      },
    
      a: function() {
        var result = [];
        // do something and push to result
        if ( this.prop ) // do something different with result
        return this.clone( result );
      },
    
      // This is the method that determines if prop = true in the chain
      b: function() {
        result = [];
        // do something and push to result
        // this time 'prop' must be 'true'
        return this.clone( result, true )
      }
    
    };
    

    I’ve used arguments.length here to check whether a parameter was passed, you could as well test against undefined or use simple arr || this.arr for always-truthy properties.

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

Sidebar

Related Questions

I have some code like this: var content = document.getElementById('myDivId'); function MyFunction() { alert(content.style.height);
I have some code that looks like this: var MyObject = function () {
I have some code in a function that goes something like this: void foo()
I have some code that looks like this: public function foo(Bar $bar) { if
I have some jQuery code like this : $(function($) { $('input.autonumeric').autoNumeric({aSep: ',', aDec: '.',vMax:'1000000000000'});
Inside my .aspx I have some JSON code that looks like this: function someFunctionName()
I have a situation like this: start(); <Some code> end(); I want start() function
I have some code like this: var mySelect = document.getElementById(mySelect); mySelect.innerHTML = <option>Loading...</option>; $.get(ajaxPage.php,
So i have some code like this: void foo (int, int); void bar (
I have some code like this: case class FunctionCommand[A](function: Function1[Array[A], Unit]) class MyClass(commands: List[FunctionCommand[_]])

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.