0

I want to check that my object properties and method or anything else is called or not? for example,

// functions 

   function app(){
      return {
        name : 'Md Tahazzot',
        info : function(){
           return this.name;
        }   
      };
   }

Now if I call this like app(), I mean In this case I am not called any of the object properties or methods. So, Is it possible to check this that I am called only the function nothing else like this app().name ?

  • Does this answer your question? [How do I test for an empty JavaScript object?](https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object) – Scircia Feb 24 '20 at 07:10
  • Where you want to check, inside the 'app' function or outside (where you are calling)? – Rahul R. Feb 24 '20 at 07:10
  • @RahulR. outside from the app function –  Feb 24 '20 at 07:15
  • If you could add why you want it, what's you motive, you may get better answer. – Rahul R. Feb 25 '20 at 08:23

2 Answers2

0

You could return a Proxy. If the proxy's getters (or setters?) are ever called, then you know that something has been done other than simply call the function - something attempted to get or set a property on the returned object:

function app() {
  const target = {
    name: 'Md Tahazzot',
    info: function() {
      return this.name;
    }
  };
  return new Proxy(target, {
    get(target, prop) {
      console.log('Get attempted');
      return target[prop];
    },
    set(target, prop, newVal) {
      console.log('Set attempted');
      return target[prop] = newVal;
    }
  });
}

console.log('Creating "a":');
const a = app();

console.log('Creating "b":');
const b = app();
b.name;

console.log('Creating "c":');
const c = app();
c.foo = 'foo';
console.log(c.foo);

If you have to do this from outside the app, then apply the same logic after the object has been returned:

function app() {
  return {
    name: 'Md Tahazzot',
    info: function() {
      return this.name;
    }
  };
}

const obj = new Proxy(app, {
  get(target, prop) {
    console.log('Get attempted');
    return target[prop];
  },
  set(target, prop, newVal) {
    console.log('Set attempted');
    return target[prop] = newVal;
  }
});

console.log('Proxy created');
obj.name;
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
0

As functions are nothing but objects in JavaScript, you can create property on function itself to store any info at function level.

You could do something like this:

 function app(){
    app.callsCount = app.callsCount || 0;
    app.callsCount++;
  return {
    name : 'Md Tahazzot',
    info : function(){
       return this.name;
    }   
  };
}

And can be used like this:

app().name
app.callsCount // 1

app()
app.callsCount //2

Keep in mind, once function is called, the count is increased, if you want to increase count on inner function call you could do that too. However it would not be straight forward to know if a property is called after app function call.

Not exactly sure what exactly you are trying to achieve.

Rahul R.
  • 4,321
  • 3
  • 23
  • 35