0

I am learning JavaScript and trying to check the caller and calle properties of Arguments object.

Here is my code

function showFunc() {
    var funcname = arguments.calee.name;
    console.log(funcname);
}
showFunc();

Now it displays an error in console saying

Uncaught TypeError: Cannot read property 'name' of undefined

Please tell me where am I going wrong.

Pang
  • 9,564
  • 146
  • 81
  • 122
Cloudboy22
  • 1,496
  • 5
  • 21
  • 39
  • it's arguments.callee.name not arguments.calee.name – curiousgeek Oct 20 '15 at 02:42
  • i think its a typo please verify – Susheel Singh Oct 20 '15 at 02:45
  • 1
    if you're learning JS, please skip over `caller` and `callee`, because they're from an ancient form of JS, and are not used in modern JS, as the information they provide do not lead to good programming patterns – Mike 'Pomax' Kamermans Oct 20 '15 at 02:46
  • @Mike I am reading Javascript: the definite Guide... 6th Edition – Cloudboy22 Oct 20 '15 at 02:49
  • See MDN: [*arguments.callee*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee) and [*arguments.caller*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/caller). – RobG Oct 20 '15 at 02:52
  • Then I strongly recommend skipping those sections. The 1998 `caller` and `callee` properties belong in 1998, not 2015. Modern JS is modular, and knowing "who callled a function" is an antipattern (your code should work regardless of who called it, or who's being called). – Mike 'Pomax' Kamermans Oct 20 '15 at 02:52
  • @Mike'Pomax'Kamermans—I understood *callee* as a way of function expressions being recursive without the need for a name. Many languages have a way of referring to the currently executing function without using its name, it's not a bad thing in itself. – RobG Oct 20 '15 at 02:53
  • So is there any other way to check for caller and callein 2015? – Cloudboy22 Oct 20 '15 at 02:55
  • @MarcAndreJiacarrini—callee still works, just not in strict mode. Caller is removed from the current specification (ECMAScript 2015) but it will still work for some time yet (but writing new code to use it isn't a good idea). – RobG Oct 20 '15 at 02:57
  • @RobG yeah but just use a name and keep your code properly maintainable, and understandable for the person who has to work on it after you're done with it (even if that means you, 3 months from now) – Mike 'Pomax' Kamermans Oct 20 '15 at 03:24

1 Answers1

2

its callee not calee

Warning: The 5th edition of ECMAScript (ES5) forbids use of arguments.callee() in strict mode. Avoid using arguments.callee() by either giving function expressions a name or use a function declaration where a function must call itself. more details on caller callee

Why was the arguments.callee.caller property deprecated in JavaScript?

function showFunc() {
    var funcname = arguments.callee.name;
    console.log(funcname);
}
showFunc();
Community
  • 1
  • 1
Susheel Singh
  • 3,824
  • 5
  • 31
  • 66