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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:12:16+00:00 2026-05-25T15:12:16+00:00

In my js code I noticed many statements like: model.set({someAttribute : !model.get(someAttribute)}); So I

  • 0

In my js code I noticed many statements like:

model.set({someAttribute : !model.get("someAttribute")});

So I thought it may be useful to define custom method in the model:

toggle: function(attr, silent){
    this.set({attr: !this.get(attr)}, silent? {silent:true} : {});
}

The problem is, javascript javascript treats ‘attr’ as an actual key name instead of formal function parameter, so the code above doesn’t work. Any suggestions how to solve this problem?
I want an elegant one-line 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-05-25T15:12:17+00:00Added an answer on May 25, 2026 at 3:12 pm

    there isn’t an elegant one-line solution for this. in order to use attr as the key, you have to declare the object literal and then set it using the [] syntax:

    toggle: function(attr, silent){
      var data = {}, value = this.get(attr);
      data[attr] = !value;
      this.set(data, {silent: silent});
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently maintaining some flex code and noticed very many functions which are declared
I'm working on some code I didn't write and noticed that there are many
I've noticed that many people include iostream and ostream in C++ programs separately, like
I've been looking at other people's JavaScript code, and I've noticed that many programmers
I noticed that many developers use Math operations as variable values, like: $time =
I was reviewing the code for KIGG and noticed that there are so many
I looked through some code and noticed that the convention was to turn pointer
I was just writing some quick code and noticed this complier error Using the
I've been working with somebody else's code and noticed that on all uses of
I noticed some code of a colleague today that initialized class variables in the

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.