Add custom methods to objects

From JSXGraph Wiki
Jump to: navigation, search

JSXGraph objects can be extended to support your own custom method. For example, to add a method called isAlmostOrigin() to the Point objects, write the following code :

 JXG.extend(JXG.Point.prototype,  {
       isAlmostOrigin: function () {
     return Math.abs(this.X()) < 0.5 && Math.abs(this.Y()) < 0.5
   }
 })

 board = JXG.JSXGraph.initBoard('box', { axis:true })
 A = board.create('point', [1, 0], {name:'A'});
 alert(A.isAlmostOrigin()) // false