2012-09-11 2 views
1

이 코드는 어떤 기능을합니까? 특히 프로토 타입 부분을 의미합니다. 전달 된 체인 객체는 CSS 이름/값 쌍의 객체입니다. defaultFactory는 이름 값 쌍을 가진 객체를 반환한다고 생각합니다. 내가 모르는 것은 프로토 타입을 설정하는 것입니다.이 프로토 타입 코드는 무엇을합니까?

function addDefaultStyleToProtoChain(chain):Object { 
    // If there's a defaultFactory for this style sheet, 
    // then add the object it produces to the chain. 
    if (defaultFactory != null) 
    { 
     var originalChain:Object = chain; 

     // from here on... 
     defaultFactory.prototype = chain; 
     chain = new defaultFactory(); 
     defaultFactory.prototype = null; 
    } 

    return chain; 
    } 

이 플렉스 4.6 SDK 상황에 대한

의의 CSSStyleDeclaration 클래스의 addDefaultStyleToProtoChain 방법, 어떤 경우에 : 동일한 체인을 무엇

var o:Object = new defaultFactory(); 
    trace(o) // {fontFamily:Arial, color:blue}; 
    trace(chain) // {color:red, fontWeight:bold} 

    defaultFactory.prototype = chain; 
    chain = new defaultFactory(); 
    trace(chain) // ??? 

?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ UPDATE ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~이 아래의 답변에 따라
것은 무슨이다 :

그것은 프로토 타입 객체의 속성을 포함하는 객체로 설정되어있을 때처럼 보이는
// create a function that returns an object 
var myDynamicDefinition:Function = function() { 
    this.name = "initialvalue"; 
    trace('this.name='+this.name); // returns this.name=initialvalue 
} 

var dynamicDefinitionInstance = new myDynamicDefinition(); // create an instance of myDynamicDefinition 
trace(ObjectUtil.toString(dynamicDefinitionInstance));// returns name = "initialvalue" 

// create a new definition and assign it to our dynamic definitions 
var newDefinition = {color:"red", name:"newObjectValue"}; 
myDynamicDefinition.prototype = newDefinition; 

// create a new instance with new definition 
var dynamicDefinitionInstance2 = new myDynamicDefinition(); // create an instance of myDynamicDefinition 
trace(ObjectUtil.toString(dynamicDefinitionInstance2)); // returns name = "initialvalue" color = "red" 

// delete name property instance 
delete dynamicDefinitionInstance2.name; 
trace(ObjectUtil.toString(dynamicDefinitionInstance2)); // returns name = "newObjectValue" color = "red" 

// set prototype to null 
myDynamicDefinition.prototype = null; 

// create a new instance 
var dynamicDefinitionInstance3 = new myDynamicDefinition(); 
trace(ObjectUtil.toString(dynamicDefinitionInstance2)); // returns name = "newObjectValue" color = "red" 

새 인스턴스가 생성 된 후에는 새 인스턴스의 속성 값이 변경되지 않습니다. 따라서 처음에는 아무런 차이가 없습니다. 이 속성이 삭제되면 프로토 타입 객체의 값이 FALLBACK으로 사용됩니다. 위의 예제 코드와 반환 값을 참조하십시오. 즉, 그 시점에서 앞으로 당신이 그 클래스의 인스턴스를 개체 대신 원래의 클래스의 새 버전을 얻을 수 있도록 기본적으로 프로토 타입을 설정

답변

1

내가 SDK를 쳐다 보면서 defaultFactory 것을 발견했다 당신이 (프로토 타입을 통해) 객체에 생성자로이 기능을 적용 할 때

newStyleDeclaration.defaultFactory = function():void 
{ 
    leftMargin = 50; 
    rightMargin = 50; 
} 

같은 기능은 그래서, 당신은 단순히 합병 한 새로운 개체를 만드는 것입니다 함수에서 선언 된 변경 사항으로 첫 번째 객체의 속성.

var def : Function = function() { 
     this.someDefChanges = "someDefChanges"; 
    } 

    var o1 = {testValueO2 : "testValueO2"}; 
    def.prototype = o1; 
    var o2 = new def(); 
    def.prototype = null; 

    // o1 = {testValueO2: "testValueO2"} 
    // o2 = {someDefChanges: "someDefChanges", testValueO2: "testValueO2"} 

희망, 나는 귀하의 질문에 바로

1

, 클래스를 수정합니다.

그래서 내 언어가 foo는의 기본 클래스가 말할 수 있습니다. foo에 메소드를 추가하려면 foo를 확장하는 클래스를 작성하는 것이 아니라 실제로 foo에 메소드를 추가하십시오. foo의 프로토 타입을 수정합니다. 그래서 코드가 실행 된 후 foo의 모든 인스턴스가 새로운 메소드를 갖게됩니다. 당신이 프레임 워크 또는 SDK를 작성하고, 잘 문서화 경우

보통 이렇게하고 싶지 않아, 그것은 원숭이 패치에 해당하지만, 당신은 명확 아마입니다.

+0

은 그래서 기존의 클래스에 속성과 메서드에 추가하거나 기존 클래스를 대체있어? 메서드가 이미 존재하면 새 속성을 병합하는 경우? 끝났습니까? –

+1

나는 그것이 실제로는 아니지만 기존 클래스를 대체한다고 생각할 것이다. 그것은 기존 클래스를 정의하는 것을 대체하고, 그 클래스를 구현하는 객체가 의미하는 바를 대체합니다. 예, 기존 방법을 무시할 수 있습니다. 나는이 용어가 "과부하"라고 믿는다. – invertedSpear