vmcruz Posted May 9, 2014 Share Posted May 9, 2014 (edited) I run into some trouble while making a class in javascript. Let's see:I have this class: var myClass = function(x, y) { this.x = x; this.y = y; this.method = function() { alert(); //example purposes } var _self = this; this.object = { objectMethod = function() { //_self returns "myClass" object //stuff goes here } }}So what I'd like to do is separate that object to be outside the class, like this:var myClass = function(x, y) { this.x = x; this.y = y; this.method = function() { alert(); //example purposes }}myClass.prototype.object = { _self : this; objectMethod : function() { //this._self returns window object //stuff goes here }}The problem is, whenever I want to use "this" reference inside the objectMethod, "this" returns the window object and not the myClass object. What do you suggest me to do?, any ideas?. Thanks in advance. Edit: So far this is the best I came out with: var myClass = function() { this.object = new object(this);}object = function(o){ this.objectMethod = function() { return o; //returns myClass object }}But feels wrong...very wrong x.x Edited May 9, 2014 by vmcruz Quote Link to comment Share on other sites More sharing options...
Noid Posted May 9, 2014 Share Posted May 9, 2014 I don't understand what your trying to do, but if this is returning the window object I suggest you read http://dailyjs.com/2012/06/18/js101-this/ Quote Link to comment Share on other sites More sharing options...
vmcruz Posted May 10, 2014 Author Share Posted May 10, 2014 Thanks for replying. Finally I made it work, sort of... var myClass = function() { this.x = 5; this.object = new myClass.object(this);}myClass.object = function(o) { this.myClass = o;}myClass.object.prototype = { objectMethod : function() { return this.myClass.x; }}var instance = new myClass();instance.object.objectMethod(); //returns 5 as I wanted.What I wanted was to be able to access myClass properties inside an object (that owns methods). I read Phaser raw code to see how Rich had accomplished that. I'm not sure if this is the exact same way Rich made it work, but so far is the one I have working. Quote Link to comment Share on other sites More sharing options...
away168 Posted May 10, 2014 Share Posted May 10, 2014 Isn't "new myClass.object(this)" will create a new "myClass.object" object with reference of previously made class (myClass)? this may jeopardize your code when "instance" variable (the new myClass()) is not present/removed, where objectMethod will unable to return x because this.myclass is null. By the way, I still don't understand why you want to access myClass properties inside an object. Maybe you could point which of Phaser's code so anyone here can get what your intention is? Quote Link to comment Share on other sites More sharing options...
vmcruz Posted May 10, 2014 Author Share Posted May 10, 2014 Isn't "new myClass.object(this)" will create a new "myClass.object" object with reference of previously made class (myClass)? 1. this may jeopardize your code when "instance" variable (the new myClass()) is not present/removed, where objectMethod will unable to return x because this.myclass is null. By the way, I still don't understand 2. why you want to access myClass properties inside an object. Maybe you could 3. point which of Phaser's code so anyone here can get what your intention is?1. It wont (I think?) because when "myClass" is created it automatically creates a new myClass.object, unless you create myClass.object directly, that would be an error. You can see it as an abstract class (or so)2. I'm making my own engine, cause I like challenges (...). When I create "myClass", it has properties needed in other objects inside that class, as stated above...I cannot access to myClass properties without reference. Again, this is the best I came out with, kinda copy Phaser (Rich) method.3. From Phaser's code:(starting line should be 19549) this.world = new Phaser.World(this); this.add = new Phaser.GameObjectFactory(this); this.make = new Phaser.GameObjectCreator(this); this.cache = new Phaser.Cache(this); this.load = new Phaser.Loader(this); this.time = new Phaser.Time(this); this.tweens = new Phaser.TweenManager(this); this.input = new Phaser.Input(this); this.sound = new Phaser.SoundManager(this); this.physics = new Phaser.Physics(this, this.physicsConfig); this.particles = new Phaser.Particles(this); this.plugins = new Phaser.PluginManager(this); this.net = new Phaser.Net(this); this.debug = new Phaser.Utils.Debug(this); this.scratch = new Phaser.BitmapData(this, '__root', 1024, 1024); Quote Link to comment Share on other sites More sharing options...
away168 Posted May 10, 2014 Share Posted May 10, 2014 Ah yess.. very interesting indeed, and congratulations to find the workaround and to share it out. Well if your intention is like what Rich does I guess it works as well. If it isn't, it could be another way to create abstract class as what you say (whatever your intention is on using this way of code). Just curious, only if you will to share; the way I see it, those codes from Phaser are alive as long as the application is open, so they are systems that will not die until the application is closed. I believe he also does it so everything can be shared one another. I am probably wrong cause I never trace it further. Is that what you want to do with that code? or are you using that for normal class declarations as well (like game objects and stuff)? Quote Link to comment Share on other sites More sharing options...
vmcruz Posted May 10, 2014 Author Share Posted May 10, 2014 I believe he also does it so everything can be shared one another. I am probably wrong cause I never trace it further. Is that what you want to do with that code? or are you using that for normal class declarations as well (like game objects and stuff)? What I'm trying to do is to share the main class properties along the "abstract" classes, since they need the main class to be instatiated in first place. I don't know how he is doing this, but the only thing my classes share is the root object (if needed) and the parent object. Per say:I have a class called "Sprite", and in order to draw "this" sprite into canvas I need to know what's the current canvas.context, so In my "SpriteManager" class I pass to "Sprite" the "Game" object which receives it when it is created in first place. To be exact, this is a portion of what I'm working, right now I'm adding effects (animations) to Sprite class./*Extract from Game Engine Mustach v1.1 - Víctor Cruz ▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄███████████▄ ▄███████████▄ ▄███████████████▄ ▄███████████████▄■▄▄▄ ▄██████████████████▀███████████████████▄ ▄▄▄■ ▀▀████████████████████▀ ▀████████████████████▀▀*/Mustach.Game = function() { this.sprite = new Mustach.SpriteManager(this);}Mustach.Sprite = function(game, key, x, y) { this.key = key; this.x = x; this.y = y; this.image = new Image(); this.game = game;}Mustach.SpriteManager = function(o) { this.game = o;}Mustach.SpriteManager.prototype = { add : function(key, spriteURL, x, y) { var sprite = new Mustach.Sprite(this.game, key, x, y); }}Btw, my engine is called "Mustach". But I don't think I'll released, at least not until I have a stable version that works for my own games. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.