Muhammad Musavi
Muhammad Musavi

Reputation: 2696

How to call a function right before object is called

Is it possible to call a function at the second an object is being called?

I have following object:

var z;
var a = {
    b: function(){
      return z * 2;
     }
    c: function(){
      return z * 3;
     }
    d: function(){
      return z * 4;
     },
    e: function(){
      return z * 5;
     }
}

var onObjectInvoke = function(){
  z = (new Date()).getTime();
}

I want to reset the value of z before a.d() is being called, here's the flow I mean, when a.d() is called:

  1. call onObjectInvoke.

  2. call a.d();

I'm looking for some kind of constructor, is there any way?

Upvotes: 0

Views: 71

Answers (4)

t.niese
t.niese

Reputation: 40862

In such a case you most certainly don't want to use z directly but invoke a function that returns the value for you. And in that function, you will either reset z or return it depending on your current use case.

Having external code that heavily modifies the behavior/outcome of a function is always a bad idea with regard to maintainability and readability.

var z;
var resetZ = true;

var a = {
  getZ: function() {
    if (resetZ) {
      z = (new Date()).getTime();
    }
    return z;
  },
  b: function() {
    return this.getZ() * 2;
  }
  c: function() {
    return this.getZ() * 3;
  }
  d: function() {
    return this.getZ() * 4;
  },
  e: function() {
    return this.getZ() * 5;
  }
}

a.d()

You for sure should not use a global resetZ, but as it is not clear how exactly you use your code it is not clear how to structure the code. Or maybe getZ should be a free function, instead of a function belonging to the object.

Upvotes: 0

Code Maniac
Code Maniac

Reputation: 37745

One simple way is to add one more layer and then access onObjectInvoke() in that layer first and return the functions from that layer as object and call the desired function on return value

  a.defaultFunc().d()

var z;
var a = {
  defaultFunc: function() {
    onObjectInvoke()
    return {
      b: function() {
        return z * 2;
      },
      c: function() {
        return z * 3;
      },
      d: function() {
        return z * 4;
      },
      e: function() {
        return z * 5;
      }
    }
  }
}
var onObjectInvoke = function() {
  console.log('reseting z')
  z = (new Date()).getTime();
}

console.log(a.defaultFunc().d())
console.log(a.defaultFunc().e())

Upvotes: 0

Julian Vizcaino
Julian Vizcaino

Reputation: 163

var z;
var a = {
    b: 2,
    c: 3,
    d: function(){
        onObjectInvoke();
        return z * 4;
    }
}

var onObjectInvoke = function() {
    z = (new Date()).getTime();
}

console.log(a.d());

Upvotes: 0

zfj3ub94rf576hc4eegm
zfj3ub94rf576hc4eegm

Reputation: 1273

Why can't you just call onObjectInvoke() before calling a.d()? Your life will be a lot easier if you allow your functions to be stateless and operate on given parameters rather than using a global z variable.

var z;
var a = {
    d: function(x){
        return x * 4;
    }
}

var onObjectInvoke = function(){
  return (new Date()).getTime();
}

z = a.d(onObjectInvoke());

Upvotes: 1

Related Questions