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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:34:13+00:00 2026-05-28T02:34:13+00:00

I got this object Person = Backbone.Model.extend({ defaults: { name: ‘Fetus’, age: 0, children:

  • 0

I got this “object”

Person = Backbone.Model.extend({
    defaults: {
        name: 'Fetus',
        age: 0,
        children: []
    },
    initialize: function() {
        this.bind("change:name", function() {
            var name = this.get("name");
            alert("Changed my name to " + name);
        });
    },
    adopt: function(newChildsName) {
        this.get("children").push(newChildsName);
    },
    setName: function(name) {
        this.set({
            name: name
        });
    }
});

Can’t the set method setName be done like this :
I tried but they don’t work

setName: function(name){
  this.get(name) = name;
}

or

setName: function(name){
  this[name].set(name);
}

Is there another way ? I can’t go around thinking the original way is too ugly to use.

  • 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-28T02:34:14+00:00Added an answer on May 28, 2026 at 2:34 am

    For clarity I am changing the argument name to newName. While:

    this.set({name: newName});
    

    means: set a value of an attribute named name with new value of newName parameter (correct), this form:

    setName: function(newName){
      this.get(newName) = newName;
    }
    

    does nothing. It gets an attribute whose name is provided in newName parameter and returns it. The assignment has no effect.

    On the other hand this:

    setName: function(newName){
      this[newName].set(newName);
    }
    

    will probably throw an error. this is a Backbone model object. It contains attributes property, so probably:

    this.attributes[newName] = newName;
    

    is more reasonable (change the attribute whose name is passed in newName parameter to newName value. However in this case the change event won’t be triggered. But let me guess, this is what you really wanted:

    setAttr: function(name, value){
      var diff = {};
      diff[name] = value;
      this.set(diff);
    }
    

    In this case you can call:

    model.setAttr('id', 7);
    

    Which is equivalent to:

    model.setAttr({id: 7});
    

    and triggers change:id correctly.

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

Sidebar

Related Questions

I've got a person object with a name and age property that implements INotifyPropertyChanged.
why i got this error : Object reference not set to an instance of
I got this síngleton cache object and it exposes an IEnumerable property which just
Got this line of code here but its not working. private void Button_Click(object sender,
I got this question from this discussion . A method call like object.m does
So I've got this audit table, looks like this: USE [DatabaseName] GO /****** Object:
I've got a snippet of IDL that looks like this: [ object, uuid(...), pointer_default(unique)
I've got this code: Entry.h #import <Foundation/Foundation.h> @interface Entry : NSObject { id object;
ive got an object that looks like this with print_r(): SimpleDOM Object ( [0]
I've got a object (Filter) that's defined something like this public class Filter {

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.