Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to tell if a function of noop in AngularJS?

I want to be able to tell if a function is noop. I was looking for a built in method such as angular.isNoop() but couldn't find anything. Is there anything that differentiates a noop?

like image 864
ialexander Avatar asked Mar 15 '23 11:03

ialexander


2 Answers

A noop is simply a function that contains no operations. You can test for a specific noop function by using ===

For example;

console.log(x === angular.noop);

Will print true if x was assign the noop from Angular, but this will not work if x is using the noop from jQuery.

To check if a variable looks like a noop. You just need to see if the function string ends with {}. You can try something like this.

console.log(angular.isFunction(x) && /\{\}$/.test(x.toString()));

The above should work even in the code is minified.

like image 103
Reactgular Avatar answered Apr 13 '23 01:04

Reactgular


A noop function has a name just like any other function. That name is noop. So you can check for it just by calling:

var theFunction = angular.noop;
theFunction.name === 'noop'
like image 43
whatoncewaslost Avatar answered Apr 13 '23 01:04

whatoncewaslost