Stack Exchange
John Resig Inheritance and Canvas .getContext
i am trying run this:
var Engine = Class.extend({
    canvas_id: 'canvas',
    canvas: '',
    context: '',
    init: function(canvas_id) {
        console.log('init: Setting this.canvas_id to ' + canvas_id);
        this.canvas_id = canvas_id;
    },
    begin: function() {
        console.log('begin: Getting element with ID ' + this.canvas_id);
        this.canvas = document.getElementById(this.canvas_id);
        console.log(this.canvas_id);
        console.log('begin: Set this.canvas', this.canvas)
        this.context = this.canvas.getContext('2d');
        this.context.fillText("Hello World!", 10, 10);
    }
});

But in console get error:
init: Setting this.canvas_id to canvas
begin: Getting element with ID canvas
canvas
begin: Set this.canvas null
> Uncaught TypeError: Cannot call method 'getContext' of null <

How fix this error?

Are you using new Engine() or calling the functions directly ?

Yes,
var engine = new Engine("canvas"); engine.begin();

I take it that the "John Resig Inheritance" in your title is in relation to this article? http://ejohn.org/blog/simple-javascript-inheritance/

Yes, i am trying get .getContext with John Resig Inheritance Library.

Do you actually have <canvas id="canvas"></canvas> in your HTML? Are you running this code after the page has loaded? Can you make a jsFiddle example?

Thanks all for comment's. I am fix it.
I am run this code:
var engine = new Engine("canvas");
and get error.
This need run:


Thanks all for comment's.

I think it might have to do with scope of that canvas variable in your case and your use of 'this'. In John Resig's example those properties are all primitive types that belong to that class. But the actual canvas object in your mark-up doesn't belong to your class instance.
Have you tried removing the "this" from both canvas and context?  
