[JavaScript] 글로벌 변수 및 글로벌 객체를 가져오는 방법
9313 단어 Node.jsJavaScript
JavaScript의 글로벌 변수 및 글로벌 객체
웹 브라우저window에서는 글로벌 변수입니다.
그 속성은 모두 전역 변수다.
이 전역 변수window는 특수 대상에서 전역 대상이라고 부른다.var a = [];
for (var i in window) {
if (window[i] === window) {
a.push(i);
}
}
console.log(a.join(', '));
단일 창의 결과입니다.self 또는 top는 모두 window와 같은 전역 대상을 가리킨다.
결과(Chrome)top, window, parent, frames, self
결과(Firefox)self, frames, parent, content, window, top
결과frames, parent, self, top, window
결과frames, parent, self, top, window
Node.js 전역 변수
Node.js의 경우global는 전역 변수입니다.
그 다음은 글로벌 대상이다.
그 속성은 모두 전역 변수다.var a = [];
for (var i in global) {
if (global[i] === global) {
a.push(i);
}
}
console.log(a.join(', '));
결실global, GLOBAL, root
다만, 그저 그렇다.
그나저나 jrunscript, jjs, 자바계의 자바스크립트에서 글로벌 대상은 어떻게 포인트를 매겨야 하는가.
JavaScript의 전역 객체를 가져오는 방법은?
그렇게 말하면 일반적으로 함수라고 부르면this 글로벌 대상이죠.
get-this-test.js function getThis1() {
return this;
}
function getThis2() {
'use strict';
return this;
}
console.log(typeof getThis1(), typeof getThis2());
// -> object undefined
ES5'use strict'를 사용하는 Strict 모드this는 undefined의 사양입니다.
안돼.eval해 볼까.
get-eval-this-test.js function getEval1() {
return eval('this');
}
function getEval2() {
'use strict';
return eval('this');
}
console.log(typeof getEval1(), typeof getEval2());
// -> object undefined
잘 될 리가 없어?
함수 소양this은 어때요?
get-func-this-test.js function getFunc1() {
return function(){ return this; }();
}
function getFunc2() {
'use strict';
return function(){ return this; }();
}
console.log(typeof getFunc1(), typeof getFunc2());
// -> object undefined
같이?
그럼 new Function 어때요?
get-global-test.js function getGlobal1() {
return new Function('return this')();
}
function getGlobal2() {
'use strict';
return new Function('return this')();
}
console.log(typeof getGlobal1(), typeof getGlobal2());
// -> object object
오, 다 했네.
여러 번 부르면 느린 것 같아서 반복해서 불러도 이미 얻은 전반적인 대상으로 돌아가야 한다.
get-global.js var g = Function('return this')();
function getGlobal() { return g; }
console.log(typeof getGlobal());
// -> object
완성
제 생각에는get-globalnpm에 있습니다.
다르다
하지만 여러 번 불린 것을 감안하면 변수를 밖으로 내놓는 것이 좋다.
끝.
P.S.
2014/10/20: 전역 변수와 전역 대상을 혼동하지 않는다는 것을 다시 기술했다.
2018/12/13: globalThis 다 된 것 같아요.크롬 71 이후곧 표준화되겠죠.
Reference
이 문제에 관하여([JavaScript] 글로벌 변수 및 글로벌 객체를 가져오는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/LightSpeedC/items/ce4b46cbdefd168b30ef
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
var a = [];
for (var i in window) {
if (window[i] === window) {
a.push(i);
}
}
console.log(a.join(', '));
top, window, parent, frames, self
self, frames, parent, content, window, top
frames, parent, self, top, window
frames, parent, self, top, window
Node.js의 경우
global는 전역 변수입니다.그 다음은 글로벌 대상이다.
그 속성은 모두 전역 변수다.
var a = [];
for (var i in global) {
if (global[i] === global) {
a.push(i);
}
}
console.log(a.join(', '));
결실global, GLOBAL, root
다만, 그저 그렇다.그나저나
jrunscript, jjs, 자바계의 자바스크립트에서 글로벌 대상은 어떻게 포인트를 매겨야 하는가.JavaScript의 전역 객체를 가져오는 방법은?
그렇게 말하면 일반적으로 함수라고 부르면this 글로벌 대상이죠.
get-this-test.js function getThis1() {
return this;
}
function getThis2() {
'use strict';
return this;
}
console.log(typeof getThis1(), typeof getThis2());
// -> object undefined
ES5'use strict'를 사용하는 Strict 모드this는 undefined의 사양입니다.
안돼.eval해 볼까.
get-eval-this-test.js function getEval1() {
return eval('this');
}
function getEval2() {
'use strict';
return eval('this');
}
console.log(typeof getEval1(), typeof getEval2());
// -> object undefined
잘 될 리가 없어?
함수 소양this은 어때요?
get-func-this-test.js function getFunc1() {
return function(){ return this; }();
}
function getFunc2() {
'use strict';
return function(){ return this; }();
}
console.log(typeof getFunc1(), typeof getFunc2());
// -> object undefined
같이?
그럼 new Function 어때요?
get-global-test.js function getGlobal1() {
return new Function('return this')();
}
function getGlobal2() {
'use strict';
return new Function('return this')();
}
console.log(typeof getGlobal1(), typeof getGlobal2());
// -> object object
오, 다 했네.
여러 번 부르면 느린 것 같아서 반복해서 불러도 이미 얻은 전반적인 대상으로 돌아가야 한다.
get-global.js var g = Function('return this')();
function getGlobal() { return g; }
console.log(typeof getGlobal());
// -> object
완성
제 생각에는get-globalnpm에 있습니다.
다르다
하지만 여러 번 불린 것을 감안하면 변수를 밖으로 내놓는 것이 좋다.
끝.
P.S.
2014/10/20: 전역 변수와 전역 대상을 혼동하지 않는다는 것을 다시 기술했다.
2018/12/13: globalThis 다 된 것 같아요.크롬 71 이후곧 표준화되겠죠.
Reference
이 문제에 관하여([JavaScript] 글로벌 변수 및 글로벌 객체를 가져오는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/LightSpeedC/items/ce4b46cbdefd168b30ef
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
function getThis1() {
return this;
}
function getThis2() {
'use strict';
return this;
}
console.log(typeof getThis1(), typeof getThis2());
// -> object undefined
function getEval1() {
return eval('this');
}
function getEval2() {
'use strict';
return eval('this');
}
console.log(typeof getEval1(), typeof getEval2());
// -> object undefined
function getFunc1() {
return function(){ return this; }();
}
function getFunc2() {
'use strict';
return function(){ return this; }();
}
console.log(typeof getFunc1(), typeof getFunc2());
// -> object undefined
function getGlobal1() {
return new Function('return this')();
}
function getGlobal2() {
'use strict';
return new Function('return this')();
}
console.log(typeof getGlobal1(), typeof getGlobal2());
// -> object object
var g = Function('return this')();
function getGlobal() { return g; }
console.log(typeof getGlobal());
// -> object
Reference
이 문제에 관하여([JavaScript] 글로벌 변수 및 글로벌 객체를 가져오는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/LightSpeedC/items/ce4b46cbdefd168b30ef텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)