아래 코드를보고 설명하십시오. 내가 뭘 잘못하고있는 걸까요?JavaScript에서 call() 메서드의 요점을 이해할 수 없습니다.
function doStuff(a, b){ return a + b + 1 ; } var myContext = { c: 1, d: 3 }; // myContext = this (result => 5) doStuff.call(myContext,myContext.c,myContext.d) // ... so why doesn't the below work? (result => NaN) doStuff.call(myContext,this.c,this.d) // To make the above work, i must replace "this" with "myContext" (result => 5)... doStuff.call(myContext,myContext.c,myContext.d) // ...which is no different to... doStuff(myContext.c,myContext.d) // ...so what was the point of call() method?
나는 두껍습니까?
'같이 Function.prototype 실제로이지고있다 .call()'은 함수 안에 this를 설정하므로, doStuff.call (myContext); 할 수있는 함수 doStuff() {return this.a + this.b +1}'이 있으면 – raser
함수의 * this * 매개 변수입니다. 그것의 [실행 컨텍스트 *] (http://ecma-international.org/ecma-262/5.1/#sec-10.3)의 일부이기 때문에 그것은 "컨텍스트"자체가 아닙니다. – RobG