Core JavaScript

An Explicit Constructor

A constructor is a function that creates an object. An object is collection of associated variables and functions. These variables and functions are collected together because they are closely related.

ExplicitConstructor.html

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net's Javascript - An Explict Constructor</title>
    <script type="text/javascript" src="ExplicitConstructor.js"></script>
  </head>
  <body onload="Initialize()">
     <canvas id="idCanvas" width="640" height ="480" style="background-color: #F0F0F0;"></canvas>
  </body>
</html>

ExplicitConstructor.js

class CCanvas {
	constructor() {
		var qCanvas = document.getElementById("idCanvas");
		this.mqContext = qCanvas.getContext("2d");
		this.mqAlpha = Math.PI/6;
		this.mqBeta = Math.PI/3;
	}
	
	DrawLine(dX1, dY1, dX2, dY2) {
		this.mqContext.globalAlpha = 1.0;
		this.mqContext.strokeStyle = "black";
		this.mqContext.beginPath();
		this.mqContext.moveTo(dX1, dY1);
		this.mqContext.lineTo(dX2, dY2);
		this.mqContext.stroke();
	}
}

function Initialize() {
	var qCanvas = new CCanvas();
	qCanvas.DrawLine(150, 400, 500, 50);
	qCanvas.DrawLine(50, 60, 600, 400);
}
 

Output

 
 

© 2007–2025 XoaX.net LLC. All rights reserved.