简体   繁体   中英

How to refer itself in a function regardless of extenal variable changes?

I have a function which has other functions defined:

var A = function(n) {
  console.log(n + A.num());
}
A.num = function() {
  return 5;
}

I want to change the name to B and delete A :

var B = A;
A = undefined;

But, when I do B() , It gives an error throws: "Cannot read property 'num' of undefined", which is completely normal and expected.

How do I handle this?

PS : Anyone who thinks that changing A.num() with this.num() , It will not work

You can always refer to the current function using its explicit name (if it has one).

So, change the definition, to name the function, and refer to it using that:

 var A = function currentFunction(n) { console.log(n + currentFunction.num()); } A.num = function() { return 5; } var B = A; A = undefined; B(10); //15 console.log(typeof currentFunction) //undefined, function name is local

Note: the abovementioned approach won't work if the function is named implicitly, but will continue to work if the function is renamed via fn.name


Alternatively, if the function isn't an arrow function, you can also use arguments.callee , that will work even with anonymous functions, but its use isn't recommended:

 var A = function (n) { console.log(n + arguments.callee.num()); } A.num = function() { return 5; } var B = A; A = undefined; B(10); //15

Unfortunately, none of the above will work with arrow functions, if you want to refer to itself, use a named bound function expression instead.

actually that's normal, because in js you can't really copy object

// Define object bar
var bar = {
   x : 'Hi'
}
console.log(bar.x);  //Hi

// Assign it to foo
var foo = bar;
console.log(foo.x);  //Hi

//But
foo.x = 'Hello!! Im foo.';
console.log(foo.x);  //Hello!! Im foo.
console.log(bar.x);  //Hello!! Im foo.

bar.x = "Nice to meet you foo!!";
console.log(foo.x);  //Nice to meet you foo!!
console.log(bar.x);  //Nice to meet you foo!!

you have to return a new one with the same value as the last

var foo = {...bar};

and this is not working because B is always calling 'num' from A that doesn't exist, One way to sever the parity would be stringify the initial function, then use a function constructor to create the clone after replacing the 'A' by 'B' but tell me more why are you facing this problem ?

 var A = function(n) { if(!this.num){ console.log("This is not available coz obj is not created"); console.log("Below error is for testing purpose"); } let a=this.num(n); console.log(a); } //use prototype here //A.num = function() { A.prototype.num=function(n){ return n+5; } //use new to create object new A(5); var B = A; A = undefined; //use new to create object new B(6); B(5);//lets try without using new keyword
 .as-console-wrapper { max-height: 100% !important; top: 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM