简体   繁体   English

无论外部变量如何变化,如何在函数中引用自身?

[英]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 :我想将名称更改为B并删除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.但是,当我执行B() ,它会抛出一个错误:“无法读取未定义的属性 'num'”,这是完全正常和预期的。

How do I handle this?我该如何处理?

PS : Anyone who thinks that changing A.num() with this.num() , It will not work PS :任何认为用this.num()改变A.num()都行不通

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注意:如果函数被隐式命名,上述方法将不起作用,但如果函数通过fn.name重命名,则将继续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:或者,如果函数不是箭头函数,您也可以使用arguments.callee ,即使使用匿名函数也可以使用,但不建议使用它:

 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其实这很正常,因为在js中你不能真正复制对象

// 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 ?这不起作用,因为 B 总是从不存在的 A 调用“num”,切断奇偶校验的一种方法是对初始函数进行字符串化,然后在将“A”替换为“A”后使用函数构造函数创建克隆'B' 但告诉我更多你为什么面临这个问题?

 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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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