当我尝试使用单个类和属性时,它可以正常工作。但是,如果我将它与多个类属性一起使用,则会抛出错误,因此mixchi
不是函数。
var sinchan = Backbone.Model.extend({}, {
himavari: function() {
return "sinchan nuhara";
}
}, {
mixchi: function() {
return "10";
}
});
console.log(sinchan.himavari());//output sinchan nuhara
console.log(sinchan.mixchi());// output TypeError: sinchan.mixchi is not a function
答案 0 :(得分:0)
我不知道你把它带到了哪里,但Backbone's extend
功能并不像那样。
extend
Backbone.Model.extend(properties, [classProperties])
要创建自己的模型类,请扩展 Backbone.Model , 提供实例属性,以及可选的 classProperties 直接附加到构造函数。
您的第一个示例有效,因为您在himavari
对象(classProperties
的第二个参数)中定义extend
,这相当于静态函数。
// this defines a custom model class
var MyModelClass = Backbone.Model.extend({
instanceFunc: function() {
return this.get('test');
},
otherInstanceFunc: function() {
return this.get('other');
}
}, {
// 'this' is not available in the following functions
staticFunc: function() {
return "static";
},
otherStaticFunc: function() {
return "other static"
}
});
// This is how you create an instance of a custom model class
var modelInstance = new MyModelClass({
test: 'test value',
other: 'other'
});
console.log(modelInstance.instanceFunc());
console.log(modelInstance.otherInstanceFunc());
// console.log(modelInstance.staticFunc());
// => TypeError: modelInstance.staticFunc is not a function
// console.log(modelInstance.otherStaticFunc());
// => TypeError: modelInstance.otherStaticFunc is not a function
// console.log(MyModelClass.instanceFunc());
// => TypeError: MyModelClass.instanceFunc is not a function
// console.log(MyModelClass.otherInstanceFunc());
// => TypeError: MyModelClass.otherInstanceFunc is not a function
console.log(MyModelClass.staticFunc());
console.log(MyModelClass.otherStaticFunc());

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
&#13;