Find out if a function is anonymous or defined in an object

I am trying to write a helper method in JavaScript. It must act differently if sent to a function or a link to a function.

I want to use it as follows:

helper('div', function () { return false; }) helper('div', obj.fn) 

What I canโ€™t understand: how to tell the difference between the two inside an auxiliary function?

I think because of this, JavaScript first evaluates obj.fn before it sends it. The only workaround I found was to send obj.fn as obj, i.e.

 helper('div', { fn: obj.fn }) 

Then I can tell the difference between the two using typeof. But I really like somehow to do this without declaring an additional object.

+4
source share
3 answers

You can use toString() to find out if a function is anonymous if it is declared as a named function and not an unnamed function assigned to a variable:

 function jim () { var h = "hello"; } function jeff(func) { var fName; var inFunc = func.toString(); var rExp = /^function ([^\s]+) \(\)/; if (fName = inFunc.match(rExp)) fName = fName[1]; alert(fName); } 

Gives you the name of the function, if any.

 jeff(function () { blah(); }); // alert: null; jeff(function joe () { blah(); }); // alert: "joe"; jeff(jack); // "jack" if jack is function jack () { }, null if jack = function() {} 

My previous edit referred to IE quirk, which was not in other browsers, and no longer works in IE since version 9. However, you can still assign named functions as properties of an object using a named function expression:

 var obj = { fn: function namedFunction () { } }; 

This works in all browsers, but IE 8 and below do not adhere to the specification, which says that a function is available only by that name inside its own block.

0
source

UPDATED * (AGAIN): I thought the toString () method could be your only way. However, it does not handle the link of an anonymous object in different ways.

This code demonstrates that:

 function acceptparam(fn){ console.log("fn.constructor = " + fn.constructor); console.log("typeof fn = " + typeof fn); console.log("fn toString " + fn.toString()); console.log("fn.prototype = " + fn.prototype); console.log("fn.prototype.constructor = " + fn.prototype.constructor); console.log("this[0] = " + this[0]); console.log("---"); } function empty(){ return ; } var x = { y : function(){return;} } acceptparam(empty); acceptparam(function(){return;}); acceptparam(xy); 

A very interesting question, without implementing your own solution, I donโ€™t think you can do it, this post helps explain why. His attitude towards a parental child relates only to one.

http://codingforums.com/showthread.php?t=134855

+2
source

I thought I would add another alternative answer, mainly because I did not want to add to the soup, which is my other answer, but also because it did not lie down with voters who did not leave constructive comments; -)

As an alternative to what you are trying to do, you can add a third parameter to the helper function:

 function helper (tagName, fn, method) { if (method) fn = fn[method]; //- Do rest of helper function here } //- Now if we pass an object method to helper function we can identify it properly helper('div', obj, "fn"); // method is obj.fn helper('div', function () { blah(); }); // Still works fine 

Itโ€™s just an offer and it works as well as itโ€™s even better than your current job.

+1
source

All Articles