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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:15:29+00:00 2026-05-18T11:15:29+00:00

What I want is to replace the instance attributes in a easy way, and

  • 0

What I want is to replace the instance attributes in a easy way, and do that inside the class itself. So I can take advantage of the constructor and don’t have to create a huge method just to update.

function Champ(champ ){
  var instance = this
  instance.id = champ.id
  // PERSONAL
  instance.name = champ.name
  instance.lore = champ.lore
  // ATTRIBUTES
  instance.attr1 = champ.attr1
  instance.attr2 = champ.attr2
  instance.fitness = champ.fitness
  // BARS
  instance.energy = champ.energy
  instance.stress = champ.stress

  function update( new_champ ){
    instance = new Champ( new_champ );
  }

  this.location = "1"

  this.update = update
}

// I will put in a simple way, how does it fail for me and how do I wanted it to behave

c = new Champ( {energy: 1, stress : 1} )
c.energy //=> 1 (OK)
c.update( { energy: 9, stress: 9} )
c.energy //=> 1 (FAIL, I wanted it to be 9)

I guess I am being really naive, is there a good way for it to work doing this sort of context replacement inside the class?

  • 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-18T11:15:29+00:00Added an answer on May 18, 2026 at 11:15 am

    Why not something like:

    function update(new_champ) {
        for(var prop in new_champ) {
            if(new_champ.hasOwnProperty(prop) && this.hasOwnProperty(prop)) {
                this[prop] = new_champ[prop];
            }
        }
    }
    

    This loops over the properties of the object you pass to the function and updates the corresponding property of the instance only if the instance has such a property.

    Btw. you should consider using prototype for creating class methods.

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

Sidebar

Related Questions

On MS SQL Server 2005, I have a table that I want to replace.
I want in a good performance way (I hope) replace a named parameter in
I want to be able to create an instance of a parent class X,
I have an ActiveRecord model class Foo that has_many Bar . I want to
I have some HTML and want to replace the src attributes of all the
I use Python 2.6 and I want to replace each instance of certain leading
Using Vim I often want to replace a block of code with a block
In the following code I want to replace every occurrence of U.S.A with united
I have a template in word (.docx) format and want to replace some placeholders
This is a simple one. I want to replace a sub-string with another sub-string

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.