Need help understanding JavaScript objects

Andrew Stavast

I am trying to understand objects in javascript. Here is the code:

var fn={};
var canvas;
var ctx;
fn.game=function(width,height,inSide,name){
    this.canvas2=document.getElementById(inSide).innerHTML = "<canvas id="+name+" style='width:"+width+";height:"+height+";'>Your browser does not support the Canvas Element.</canvas>";
    this.canvas=document.getElementById(name);
    this.ctx=this.canvas.getContext("2d");
    document.getElementById(inSide).style.width=width;
    document.getElementById(inSide).style.height=height;
    canvas=document.getElementById(name);
    ctx=this.canvas.getContext("2d");
    this.width=width;
    this.height=height;
    canvas.width=width;
    canvas.height=height;
    this.add={

    };
    this.add.state=function(name){
        this[name]=3;
    };
};


var game=new fn.game(640,480,"game","canvas");

game.addState("play");

when I am referencing this["name"] I am trying to refer this to fn.game, but that dous not work because this references the most local object. Any ideas on how to do this?

topheman

As you said, it references the most local object, to do what you explained :

...
fn.game=function(width,height,inSide,name){
    var that = this;//expose this of fn.game to this scope
    ...
    this.add={

    };
    this.add.state=function(name){
        that[name]=3;//access this of fn.game
    };
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Need help in understanding Scale

From Dev

Need help understanding this operation

From Dev

I need help understanding javascript ||. Is it a Logical 'OR' or not

From Dev

Need Help Understanding Javascript Closure

From Dev

Need Help Understanding these SQL Results

From Dev

Need help understanding this line

From Dev

Need help understanding how arrays work in javascript for objects

From Dev

Need help understanding instance variables

From Dev

Dagger: need help in understanding

From Dev

Need help understanding LINQ in MVC?

From Dev

Need help understanding threads in Java

From Dev

Need help understanding CIDR and submask

From Dev

Need help understanding this Perl snippet

From Dev

Need help understanding .getJSON() behavior

From Dev

need help understanding ngRepeat with filter

From Dev

Need help understanding this perl code

From Dev

I need some help understanding object literal functions in JavaScript

From Dev

Need help understanding the Shadow DOM

From Dev

Need help in understanding this SQL query

From Dev

Need help understanding a programming challenge

From Dev

Need help understanding direction formula

From Dev

need help understanding enum and arrays

From Dev

Need help understanding function invocation in JavaScript

From Dev

Need help understanding function bind when dealing with objects

From Dev

Need help understanding this Ngrx effect

From Dev

Need help understanding this Ngrx effect

From Dev

I need help understanding javascript ||. Is it a Logical 'OR' or not

From Dev

Need Javascript syntax help returning array of objects

From Dev

Need help understanding an algorithm

Related Related

HotTag

Archive