0
'Mixin Pattern'을 기반으로 예제 코드를 만들고 싶습니다.밑줄, _.extend 키워드를 사용하는 Mixin 패턴
다음과 같은 코드가 있습니다.
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();
}
});
});
이 코드를 실행하면 'testA.c는 함수가 아닙니다'라는 오류가 발생합니다. 내가 공부 한 몇 가지 예제 코드로 판단하면 이것이 효과가있다. 이 코드가 가능한 한 상세하게 작동하지 않는 이유에 대해 알려주십시오.
'_c' 속성을 정의했지만'c' ('testA.c()')를 호출했습니다. 나는 그들이 같아야한다고 생각한다. –
변경했습니다 ...하지만 같은 오류가 발생하지 않습니다. 나는 또 다른 이유가 있다고 생각한다. –