context

8 views
Skip to first unread message

Cyrille Piatecki

unread,
Dec 13, 2024, 10:08:23 AM12/13/24
to JSXGraph
Could somebody explain for a non specialist what is context in

JXG.Math.Numerics.Newton(f, x, context)

with an exemple.

Jens Axel Søgaard

unread,
Dec 13, 2024, 10:27:47 AM12/13/24
to jsxg...@googlegroups.com
First of all, let's take a look at the documentation:

image.png

The context is optional - it is only needed if the function f is an "extract" method.

In JavaScript functions calls and method calls are slightly different.

A method call such as obj.f(arg1)  will
  - evaluate obj
  - extract the method f
  - set `this` to the result of evaluating obj
  - invoke the body of the method f with argument arg1.

The body of f can thus use `this` to refer to its object.

But what if we first extract an method and then call it, like this:

   var g = obj.f
   g(arg1)

The problem is now that the call g(arg1) is a function call instead of a method call.
Therefore `this` is not set to object obj before invoking the body of g.
Instead `this` will refer to a global object.

Back to Newton. If the function f is a standard function (which implies
there are no references to this in its body), then context is not supplied.

If the function f is an extracted method, then Newton needs to know
the object it was extracted from in order to set `this` before invoking
the body of f.

See more on `this` here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

--
Jens Axel Søgaard

Reply all
Reply to author
Forward
0 new messages