admin管理员组

文章数量:1290958

I have static properties that I would like to access from instances of my Backbone.Model objects. I know I could hardcode the parent constructor to call the method, but this prevents me from having polymorphic static functions. For example, I would like to be able to override the foo function in ExtendedInventory if necessary, without having to change any other code.

var Inventory = Backbone.Model.extend({},
    //STATIC
    {
        foo: function() {
            alert('bar');
        }
    });

var i = new Inventory({});
i.constructor.foo(); //This works!

var ExtendedInventory = Inventory.extend({});

var ei = new ExtendedInventory({});
ei.constructor.foo(); //THIS DOES NOT WORK


//How do I generically access the `Inventory.foo()` function via the `ei` object. I would 

I have static properties that I would like to access from instances of my Backbone.Model objects. I know I could hardcode the parent constructor to call the method, but this prevents me from having polymorphic static functions. For example, I would like to be able to override the foo function in ExtendedInventory if necessary, without having to change any other code.

var Inventory = Backbone.Model.extend({},
    //STATIC
    {
        foo: function() {
            alert('bar');
        }
    });

var i = new Inventory({});
i.constructor.foo(); //This works!

var ExtendedInventory = Inventory.extend({});

var ei = new ExtendedInventory({});
ei.constructor.foo(); //THIS DOES NOT WORK


//How do I generically access the `Inventory.foo()` function via the `ei` object. I would 
Share Improve this question asked Jun 28, 2011 at 18:39 aw crudaw crud 8,89121 gold badges76 silver badges117 bronze badges 3
  • So, this works but I do not understand it very well and it only works in Firefox: ei.constructor.prototype.__proto__.constructor.foo() – aw crud Commented Jun 28, 2011 at 18:49
  • 1 I wrote a blog post with examples of using static members with backbone that might be helpful to anyone ing across this question. taurenmills.wordpress./2011/10/08/… – Tauren Commented Oct 11, 2011 at 5:03
  • +1 for drawing attention to class properties – Chuck Commented Mar 25, 2013 at 23:21
Add a ment  | 

2 Answers 2

Reset to default 5

Hmm. Though the code above does work, I wouldn't leave it that way. If the function is logically accessible through an object of the class, then define an instance method in the base class that calls the class/"static" function. This makes the code cleaner and clearer, I think (plus, clients don't have to remember the somewhat arcane syntax):

var Inventory = Backbone.Model.extend({
    foo: function() {
        this.constructor.foo();
    }
}, {
    foo: function() {
        alert('bar');
    }
});

var i = new Inventory({});
i.foo(); //This works!

var ExtendedInventory = Inventory.extend({});

var ei = new ExtendedInventory({});
ei.foo();

What doesn't work exactly, in your example? In both Firefox and IE, I get two popups with 'bar', which looks like the intended result? The static part of it appears to behave fine as well, see this jsfiddle.

(this is with the HEAD version of Backbone, btw; don't know if that makes a difference)

本文标签: