2012-09-26 2 views
1

"Ext.applyIf"기능 및 이중 파이프에 대한 참조는이 기사를 참조하십시오.ExtJS 4 : ExtJS 코드에서 두 개의 파이프가 적용되는 것은 무엇입니까?

http://docs.sencha.com/core/manual/content/utility.html

http://homepage.ntlworld.com/kayseycarvey/controlflow4.html

누군가는이 논리에서는 ExtJS 프레임 워크에서 무엇을하고 있는지 설명 할 수 있습니까? 파이프 (특히)가있는 첫 번째 라인에서 제 해석이 정확한지 확인하고 싶습니다.

 var params = Ext.applyIf(operation.params || {}, this.extraParams || {}), request; 
     params = Ext.applyIf(params, this.getParams(params, operation)); 
     if (operation.id && !params.id) { 
      params.id = operation.id; 
     } 

ASP.NET ASMX 사용자 지정 서버 프록시 클래스에서 촬영 :

Ext.define('Ext.ux.AspWebAjaxProxy', { 
    extend: 'Ext.data.proxy.Ajax', 
    require: 'Ext.data', 

    buildRequest: function (operation) { 
     var params = Ext.applyIf(operation.params || {}, this.extraParams || {}), request; 
     params = Ext.applyIf(params, this.getParams(params, operation)); 
     if (operation.id && !params.id) { 
      params.id = operation.id; 
     } 

     params = Ext.JSON.encode(params); 

     request = Ext.create('Ext.data.Request', { 
      params: params, 
      action: operation.action, 
      records: operation.records, 
      operation: operation, 
      url: operation.url 
     }); 
     request.url = this.buildUrl(request); 
     operation.request = request; 
     return request; 
    } 
}); 

답변

3
  1. 이중 파이프는 기본값을 지정하는 자바 스크립트 트릭입니다. Evan이 제안한 것처럼 자주 null 포인터 문제를 피하기 위해 사용됩니다. var a = b||{}의 예제에서 a는 b가 null이거나 정의되지 않은 경우에도 null이 아니므로 보장됩니다. 기본 폴백은 a = {} (빈 객체)입니다.
  2. ApplyIf 메서드는 기존 속성을 덮어 쓰지 않도록주의하여 원본에서 대상으로 속성을 복사합니다. 다음 예제에서 extraParams 속성은 params에 정의 된 속성이없는 한 params 속성에 추가됩니다.

    Ext.applyIf (PARAMS, extraParams)

실제 코드는 다음과 같습니다 extraParams는 조심스럽게 널 포인터 문제를 방지 객체를 PARAMS params가 덮어 쓰기를하지 않도록주의하는 추가

Ext.applyIf(operation.params || {}, this.extraParams || {}), 

.

1

이 고려 :

function foo(cfg) { 
    cfg = cfg || {}; 
    console.log(cfg.x); 
} 

foo({x: 1}); 
foo(); 

을 그래서 본질적으로 CFG가 전달 경우 우리는 말을하는지 메서드가 "거짓"(read, undefined 또는 null) 인 경우 빈 객체로 설정하므로 "x"속성을 읽을 때 참조 오류가 발생하지 않습니다.