Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript:使用apply调用函数时获取'this'的值_Javascript_Node.js_This_Call - Fatal编程技术网

javascript:使用apply调用函数时获取'this'的值

javascript:使用apply调用函数时获取'this'的值,javascript,node.js,this,call,Javascript,Node.js,This,Call,当我打电话时 var fun = function(input) { console.log(!!this, this, typeof(this), Object.keys(this), !this.toString(), this.constructor.name); // logs: true [Boolean: false] object [] false Boolean if(!this) return input; return this; } var someVar

当我打电话时

var fun = function(input) {
  console.log(!!this, this, typeof(this), Object.keys(this), !this.toString(), this.constructor.name); 
  // logs: true [Boolean: false] object [] false Boolean
  if(!this) return input;
  return this;
}
var someVar = false;
var result = fun.apply(someVar, ["TEST"]);
我希望结果是“TEST”,但结果是控制台中打印为[Boolean:false]的奇怪对象

问题是,如果
this
(作为
someVar
传入)具有某种错误值,那么在本例中返回“TEST”,那么检查函数内部最优雅的方法是什么?

代码正在节点js中运行。

您需要在函数中添加代码,以便其
值不会被强制为对象:

var-fun=函数(输入){
“严格使用”;
console.log(!!this,this,typeof this);
如果(!this)返回输入;
归还这个;
}
var-someVar=false;
var result=fun.call(someVar,“TEST”)
您需要在函数中添加,以便其
值不会被强制为对象:

var-fun=函数(输入){
“严格使用”;
console.log(!!this,this,typeof this);
如果(!this)返回输入;
归还这个;
}
var-someVar=false;
var result=fun.call(someVar,“TEST”)