javascript get context code example

Example 1: context javascript

console.log(this);
// Window {0: Window, window: Window, self: Window, document: document, name: "", location: Location, …}

class MyClass {
	constructor(x, y){
    	this.x = x;
      	this.y = y;
      	this.sum = x + y;
    }
	printThis(){
    	console.log(this);
    }
}

myClass = new MyClass(3, 4);
myClass.printThis();
// MyClass {x: 3, y: 4, sum: 7}

Example 2: javascript getcontext

var canvas = document.getElementById("canv");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(10, 10, 80, 80);

Example 3: canvas.getContext

The HTMLCanvasElement.getContext() method returns a drawing context on the
canvas, or null if the context identifier is not supported, or the canvas has
already been set to a different context mode.
Later calls to this method on the same canvas element, with the same contextType
argument, will always return the same drawing context instance as was returned
the first time the method was invoked. It is not possible to get a different
drawing context object on a given canvas element.