I cannot find the reason why this doesn’t work:
var c=document.getElementById("myCanvas"),
ctx=c.getContext("2d");
ctx.moveTo(0,0);
ctx.lineTo.apply(this, [100, 100]);
ctx.stroke();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Right now,
thisis eitherwindow… which really doesn’t know what to do with.lineTo, orthisis whatever object owns the function that this code is wrapped in.If this code is not wrapped in a function (or even if it is, if that function is anything other than the property of an object,
this===window).eg:
That code would call
.lineTousingMyObjasthis.If
MyObjdidn’t know what to do with that information, or.lineTocouldn’t find the properties it needed attached toMyObj, then it will fail.In almost all other cases of reading from
this, instead of writing to it,thisrefers towindow.Another common pitfall: functions inside of functions.
in order to scope
thisproperly in this case, save it as a variable in the first function, and reference it in the second.