자바스크립트 실행 순서에 관해

var textObj = {
fn : function(obj){
console.log(obj);
obj.context = “b”;
}
}
textObj.fn.call(this,{‘test’:‘a’});

위 구문을 실행했을때
분명 console.log(obj); 는 obj.context = “b”; 전에 실행되었는데
막상 출력결과를 보면 log에 context를 포함하고 있습니다.
그 이유가 궁금합니다.

코드는 코드블럭으러 감싸주시는게 답변하시는 분들께도 보기가 편할 것 같아요~

var textObj = {
	fn : function(obj) {
		console.log(obj);
		obj.context = 'b';	
	}		
}
textObj.fn.call(this, {'test':'a'}); 
// {test: "a"}
var textObj = {
	fn : function(obj) {
		obj.context = 'b';	
		console.log(obj);
	}		
}
textObj.fn.call(this, {'test':'a'});
// {test: "a", context: "b"}

유성님께서 생각하시는 대로 출력되는 것이 맞는 것 같아요.
혹시 크롬 개발자 도구 등에서 출력 결과 클릭하셔서 보신건가요??
그렇다면 해당 객체를 참조해서 보여주기 때문에 context가 보입니다.
(실제 console.log 출력 결과는 아닙니다)

1개의 좋아요

console.log 는 정확하게 그당시의 객체 상태를 보여주지 않습니다.
JSON.stringify 를 쓰면 그당시 모습을 확인할 수 있습니다.
console.log(JSON.stringify(obj));
debugger를 거는게 더 낫긴한것같습니다.