이 범위 해상도 가변 정지가 생성되기 때문에 'VAR'의 경우에 작동합니다. 'var'가 없으면 스코프 체인을 들여다 보았고이라고 말합니다.
// This is fine because assignment always sets a property value
// in this case (no 'var ga') it's the same as window.ga = window.ga || []
ga = window.ga || []
또는 :
// Once again, only the lookup is affected with "running off"
// the lookup chain. It's not that the variable has no value --
// the problem is there IS NO VARIABLE.
if (!window.ga) {
ga = []
}
심지어이 : 두 경우 모두에 내가 명시 적으로 ga
를 호출하는 것이
// First line is the same as window.ga = window.ga,
// but now the property ga is guaranteed to exist on the window object --
// the VALUE of the property may (still) be undefined
ga = window.ga
ga = ga || []
주 당신이 정말로 원하는 경우는 전역 변수를 사용하는 window
(전역) 개체의 속성
당신은 여기에 세부 사항에 대해 읽을 수 있습니다 Identifier Resolution, Execution Contexts and Scope Chains.
문제가되지 않는 범위 내에서 VAR의 배치.다음은 모두 동일합니다
가
var ga
ga = ga || []
var ga = ga || []
ga = ga || []
var ga
당신은'var ga = ga || [] 왜냐하면'ga = ga || [];'오류 (S.Mark가 지적한 것처럼). – R0MANARMY
감사합니다. 질문을 업데이트했습니다. –
나는 그것이 ga = ga ||라고 생각했다. 오. 라 = 라; want (yourBadRomance); –