클로저에 정의 된 함수에서 객체를 생성 할 수없는 이유는 무엇입니까?
var outer = function() {
var constructor = function() {
this.foo = 1;
};
return constructor;
};
// Should be: { foo: 1 }, but is: undefined
var constructorObject = new outer()();
// This works
var c = outer();
var constructorObject = new c();
다음과 같이 외부 함수 호출을 괄호로 묶어야합니다.
var constructorObject = new (outer())();
// ^ ^ parenthesis here
console.log(constructorObject); // constructor {foo: 1}
console.log(constructorObject.foo); // 1
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다