I want to make an example code based on 'Mixin Pattern'.
I have a code like below.
define(["jquery","underscore", "backbone"], function($, _,Backbone) {
//Mixin :
//
/* Sometimes you have the same functionality for multiple objects
* and it doesn’t make sense to wrap your objects in a parent object.
* For example, if you have two views that share methods but don’t
* – and shouldn’t – have a shared parent view.
*/
//I can define an object that has attributes and methods that can be shared across different classes.
//This is called a mixin.
//Mixin Object
var C = Backbone.Model.extend({
_c: function() {
console.log("We are different but have C-Method shared");
}
});
//To be Mixin-ed Object-1
var A = Backbone.Model.extend({
a: 'A',
});
//To be Mixin-ed Object-2
var B = Backbone.Model.extend({
b: 'B'
});
//underscore
_.extend(A.prototype, C);
_.extend(B.prototype, C);
return Backbone.Model.extend({
initialize: function(){
var testA = new A();
testA.c();
var testB = new B();
testA.c();
}
});
});
If I run this code, there comes an error saying 'testA.c is not a function'. Judging by some example codes which I studied, This should work. Can you please let me know about the reason this code doesn't work as detail as possible?
Aucun commentaire:
Enregistrer un commentaire