Bug: attributes inherited from Object.prototype #1495

Closed
paulmillr opened this Issue Jul 18, 2012 · 4 comments

Comments

Projects
None yet
4 participants
@paulmillr
Contributor

paulmillr commented Jul 18, 2012

john = new Backbone.Model()

building1 = new Backbone.Model()
building1.set {constructor: john}
building1.has 'constructor'  # => true

building2 = new Backbone.Model()
building2.has 'constructor'  # => true. WTF?
@tbranyen

This comment has been minimized.

Show comment
Hide comment
@tbranyen

tbranyen Jul 19, 2012

Collaborator

Whole bunch of issues here, model.has("hasOwnProperty") == true as well. Any ideas on a patch other than using hasOwnProperty ?

Collaborator

tbranyen commented Jul 19, 2012

Whole bunch of issues here, model.has("hasOwnProperty") == true as well. Any ideas on a patch other than using hasOwnProperty ?

@paulmillr

This comment has been minimized.

Show comment
Hide comment
@paulmillr

paulmillr Jul 19, 2012

Contributor

I'm using Object.create(null) in cases like that, we can check for its support and use this if it's supported. One-line patch:

this.attributes = typeof Object.create === 'function' ? Object.create(null) : {};

A patch that will support old ie is much more sophisticated. _.has(this.attributes, attr) should be placed everywhere etc.

Contributor

paulmillr commented Jul 19, 2012

I'm using Object.create(null) in cases like that, we can check for its support and use this if it's supported. One-line patch:

this.attributes = typeof Object.create === 'function' ? Object.create(null) : {};

A patch that will support old ie is much more sophisticated. _.has(this.attributes, attr) should be placed everywhere etc.

@jashkenas

This comment has been minimized.

Show comment
Hide comment
@jashkenas

jashkenas Oct 1, 2012

Owner

Yep -- not so much a bug as a known limitation. Take care with your attribute keys. Making your proposed patch would cause working code in webkit / firefox to error in internet explorer, a situation we'd like to avoid as much as possible.

Owner

jashkenas commented Oct 1, 2012

Yep -- not so much a bug as a known limitation. Take care with your attribute keys. Making your proposed patch would cause working code in webkit / firefox to error in internet explorer, a situation we'd like to avoid as much as possible.

@ZaValera

This comment has been minimized.

Show comment
Hide comment
@ZaValera

ZaValera Oct 18, 2015

Why do not use:

    get: function(attr) {
      if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) {
        return this.attributes[attr];
      } else {
        return void 0;
      }
    }

Why do not use:

    get: function(attr) {
      if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) {
        return this.attributes[attr];
      } else {
        return void 0;
      }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment