2

I was thinking that having a function with variables and methods would be able to allow the subfunctions to reference the variables. Maybe I am doing it wrong, and would like some advice on how to properly fomat this stuff.

function somefunction(callback) {
    var x,y,z;
    var timervar = setInterval(function() {
        //...
        callback().always(function() {
            x++;
        });
        //...
    }, 1);
}

How would i properly relate the X?

Edit:

I am doing some iteration within a while loop. x,y,z are variables which are to store counter information. I was more or less incremementing or decremementing a variable when callback finished execution.

The reason why it isnt calling more callback() is because it is in while loop. WHich is fine and dandy as it is related to the X value. It seems that after a certain point, it exits the scope of the while loop and waits for .always() to be fired off so it would then be able to resume the while loop. The while loop is set within the timer, so it checks every 10ms if it is ready to keep looking.

Final Edit: always wasnt firing because i forgot the return in callback() so it was never recognized as finished. ._.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

5 Answers5

3

Another way is to make self contained module:

somefunction = (function() {
    var x,y,z;

    return {
        callback: function(){
            return x++;
        }
    }
});

You can explore a great documentation on JavaScript Design Patterns on http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript

Apoorv Saxena
  • 4,086
  • 10
  • 30
  • 46
2

Try something like

var SomeObj = {
  x: 0,
  myFunc : function(){
     SomeObj.x++;
  }
}
Sully
  • 14,672
  • 5
  • 54
  • 79
  • @kmatheny How so? `SomeObj` can't be reused. It's a single object – Ian Sep 26 '12 at 20:47
  • @ianpgall recall the constructor. SomeObj = new SomeObj(); – Fallenreaper Sep 26 '12 at 20:50
  • @Fallenreaper: Problem with that is, with this syntax, `SomeObj` is no longer a function -- which means it can't serve as a constructor. – cHao Sep 26 '12 at 20:58
  • @Fallenreaper Yeah I'm not sure what you're talking about, you can't call an object... – Ian Sep 26 '12 at 21:00
  • my bad. I was thinking that it maintained some sort of constructor status. – Fallenreaper Sep 26 '12 at 21:12
  • 1
    @Fallenreaper You're absolutely right. Long day at the office. Didn't even catch that it was var SomeObj = {} and not var SomeObj = function() {}. I commonly use var SomeObj = (function(){return{x:0,myFunc:function(){x++;}}}; – kmatheny Sep 26 '12 at 22:15
1

Try this:

function createObject(o) {
    function F() {}
    F.prototype = o;
    return new F();
}

Taken from Prototypal Inheritance: http://javascript.crockford.com/prototypal.html

var SomeObj = {
  x: 0,
  myFunc : function(){
     SomeObj.x++;
  }
}

var newObj = createObject(SomeObj);

Maybe this post would help? https://stackoverflow.com/a/3075818/566012

Community
  • 1
  • 1
kmatheny
  • 4,042
  • 1
  • 19
  • 12
0

I used the following implementation of inheritance in JS

/**
* inheritance
* 
* @author Kevin Lindsey
* @version 1.0
* 
* copyright 2006, Kevin Lindsey
* 
*/

// namespace placeholder
TWE = {};

/**
* A function used to extend one class with another
* 
* @param {Object} subClass
*       The inheriting class, or subclass
* @param {Object} baseClass
*       The class from which to inherit
*/
TWE.extend = function(subClass, baseClass) {
    function inheritance() { }
    inheritance.prototype = baseClass.prototype;

    subClass.prototype = new inheritance();
    subClass.prototype.constructor = subClass;
    subClass.baseConstructor = baseClass;
    subClass.superClass = baseClass.prototype;
}

Then you could do something like:

function myBaseClass() {
this._datamember1 = ....;
//more data members
}

function mySubClass() {
 mySubClass.baseConstructor.call(this);   

//list additional data members here
}

TWE.extend(mySubClass, myBaseClass);

see more here http://www.kevlindev.com/tutorials/javascript/inheritance/index.htm

sarsnake
  • 26,667
  • 58
  • 180
  • 286
0

I would say that you can assign

var me = this;

Then in your code you can use me.x, me.y etc.

Zlatko
  • 18,936
  • 14
  • 70
  • 123
  • Forgot to say: for javascript functiom, try thinking of their scope (what 'this' is referring to) as your objects. The scope is called a closure. This is not a correct interpretation in many ways, but it may help you understand how the x is inherited here. – Zlatko Sep 26 '12 at 20:55