Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: attributes inherited from Object.prototype #1495

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

Bug: attributes inherited from Object.prototype #1495

paulmillr opened this issue Jul 18, 2012 · 4 comments
Labels

Comments

@paulmillr
Copy link
Contributor

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
Copy link
Collaborator

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

@paulmillr
Copy link
Contributor Author

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
Copy link
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.

@ZaValera
Copy link

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
Labels
Projects
None yet
Development

No branches or pull requests

4 participants