Created
October 20, 2013 14:33
-
-
Save trungdq88/7070431 to your computer and use it in GitHub Desktop.
Class for Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================ | |
// Register Namespace | |
//------------------------------------------------------------ | |
var Shape = Shape||{}; | |
//============================================================ | |
// Constructor - MUST BE AT TOP OF FILE | |
//------------------------------------------------------------ | |
Shape.Rectangle = function Shape_Rectangle(width, height, color){ | |
this.Width = width; | |
this.Height = height; | |
this.Color = color; | |
} | |
//============================================================ | |
// Member Functions & Variables | |
//------------------------------------------------------------ | |
Shape.Rectangle.prototype = { | |
Width: null | |
,Height: null | |
,Color: null | |
,Draw: function Shape_Rectangle_Draw(canvasId, x, y){ | |
var canvas = document.getElementById(canvasId); | |
var context = canvas.getContext("2d"); | |
context.fillStyle = this.Color; | |
context.fillRect(x, y, this.Width, this.Height); | |
} | |
} | |
//============================================================ | |
// Static Variables | |
//------------------------------------------------------------ | |
Shape.Rectangle.Sides = 4; | |
//============================================================ | |
// Static Functions | |
//------------------------------------------------------------ | |
Shape.Rectangle.CreateSmallBlue = function Shape_Rectangle_CreateSmallBlue(){ | |
return new Shape.Rectangle(5,8,'#0000ff'); | |
} | |
Shape.Rectangle.CreateBigRed = function Shape_Rectangle_CreateBigRed(){ | |
return new Shape.Rectangle(50,25,'#ff0000'); | |
} | |
// Putting the class to work | |
alert("A rectangle has "+Shape.Rectangle.Sides+" sides."); | |
var r1 = new Shape.Rectangle(16, 12, "#aa22cc"); | |
r1.Draw("painting",0, 20); | |
var r2 = Shape.Rectangle.CreateSmallBlue(); | |
r2.Draw("painting", 0, 0); | |
Shape.Rectangle.CreateBigRed().Draw("painting", 10, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment