有骨干的类构造函数

发布于 2024-12-26 00:17:53 字数 217 浏览 0 评论 0原文

任务

可以将命名参数传递给新的 bb 支持的对象

示例

initialize : function(foo, bar, baz){
  this.foo = foo;
  ...
}


new Foo(value0, value1, value2)

问题

是否可以在不修改库源代码的情况下?

task

to make it possible to pass named paramteres to new bb-backed objects

example

initialize : function(foo, bar, baz){
  this.foo = foo;
  ...
}


new Foo(value0, value1, value2)

question

Is it possible without modifying the library source code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酒忠诚 2025-01-02 00:17:53

如果您检查源代码,您会发现模型像这样调用 initialize

Backbone.Model = function(attributes, options) {
  // ...
  this.initialize(attributes, options);
};

但是 Collection、Router 和 View 像这样调用它:

this.initialize.apply(this, arguments);

众所周知,apply 执行以下操作:

使用给定的 this 值和以数组形式提供的参数来调用函数。

参数是:

与传递给函数的参数相对应的类似数组的对象。

因此,对于模型,您必须使用标准 文档化接口,但对于其余的,您可以执行类似这样的操作:

var View = Backbone.View.extend({
    initialize: function(a, b, c, d) {
        // ...
    }
});
new View('where', 'is', 'pancakes', 'house?');

演示: http://jsfiddle.net/ambigously/5ZD6z/

请注意,这样做违反了记录集合RouterView 接口,因此如果使用,请不要感到惊讶这未记录的行为会导致新的有趣的错误或升级后神秘的中断。我建议坚持使用已记录的接口,如果它真的让您很烦恼,请编写构造函数:

function make_thing(a, b c) {
    return new Thing({
        a: a,
        b: b,
        c: c
    });
}

然后继续进行更高效的事情。

If you check the source you'll see that Model calls initialize like this:

Backbone.Model = function(attributes, options) {
  // ...
  this.initialize(attributes, options);
};

But Collection, Router, and View call it like this:

this.initialize.apply(this, arguments);

And as we all know, apply does this:

Calls a function with a given this value and arguments provided as an array.

and arguments is:

An array-like object corresponding to the arguments passed to a function.

So for models you're stuck with the standard documented interface but for the rest you can do things like this:

var View = Backbone.View.extend({
    initialize: function(a, b, c, d) {
        // ...
    }
});
new View('where', 'is', 'pancakes', 'house?');

Demo: http://jsfiddle.net/ambiguous/5ZD6z/

Note that doing this violates the documented Collection, Router, and View interfaces so don't be surprised if using this undocumented behavior causes new and interesting bugs or breaks mysteriously after an upgrade. I'd recommend sticking to the documented interfaces and if it really bothers you so much, write constructor functions:

function make_thing(a, b c) {
    return new Thing({
        a: a,
        b: b,
        c: c
    });
}

and move on to more productive things.

橘香 2025-01-02 00:17:53

您可以将 JavaScript 对象作为参数传递到构造函数中,请查看 Model.extend 的文档< /a>:

MyModel = Backbobe.Model.extend({
    initialize: function(options){
        this.foo = options.foo;
    }
});

//pass your options as second parameter in the constructor call
// the first parameter is used as your model data    
var myModel = new MyModel({},{foo:bar});

You can pass JavaScript objects as parameters into the constructor, take a look at the docu for Model.extend:

MyModel = Backbobe.Model.extend({
    initialize: function(options){
        this.foo = options.foo;
    }
});

//pass your options as second parameter in the constructor call
// the first parameter is used as your model data    
var myModel = new MyModel({},{foo:bar});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文