下面的错误意味着什么,我应该如何修复它?
$ npm test
> location-autosuggest@3.0.0 test /Users/mishamoroshko/location-autosuggest
> mocha test --compilers js:babel/register
compareKeys()
should return -1
1) when first key is different
0 passing (301ms)
1 failing
1) compareKeys() should retu
function foo() {
if (arguments.callee.self)
return arguments.callee.self;
arguments.callee.self = this;
//do sth
}
我能理解它的名字是这样的:
var a = foo();
当foo被执行时,arguments.callee本身就是foo。所以它把它传递给未定义的变量self。下一次当另一个函数调用foo时,它将返回以下内容。显然,这是可行的。
当它被这样调用时,事情似乎变得更棘手了:
var b = new foo();
我认为js引擎创建
我调用该命令:
arguments.callee
我得到了错误:
(node:1956) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
(nod
有一个老把戏()可以捕捉到将构造函数作为函数调用,即忘记new关键字。下面的代码,或者类似的代码,放在顶部的每个构造函数中。
if (!(this instanceof arguments.callee)) {
throw Error("Constructor called as a function");
}
当您需要"use strict";时,有哪些替代方案?
它的泛型性质能被保留吗?或者我们必须使用构造函数的名称来代替arguments.callee
使用普通的ECMAScript,你可以这样做,
function f () { console.log(this.constructor.name); }
new f() // outputs `f`
然而,稍微修改一下,
function* f () { console.log(this.constructor.name); }
var g = new f();
g.next() // outputs `GeneratorFunctionPrototype`
有没有办法获得生成器的名称(f)?
function MyFunction() {
closure = function() {
alert('my parent function name is: '/* now what here ? */);
}
closure();
}
MyFunction();
结果应为my parent function name is: MyFunction
(致版主:我不知道为什么stackoverflow阻止我发送这个问题,声称它不符合质量标准。我是否必须键入一些多余的文本才能发送此邮件。)
在google-apps-script脚本文件中可以设置'use strict‘吗?我创建了一个函数来测试这一点
function a () { //logs - function a
"use strict";
Logger.log(arguments.callee);
}
如果强制使用严格模式,我认为此语句将抛出TypeError。
我希望能够将属性分配给函数本身内部的函数。我不想把它赋给调用的对象。所以我想要做的等价物:
var test = function() {
return true;
};
test.a = 'property on a function';
alert(test.a);
而不是这样,其中属性被分配给全局对象:
var testAgain = function() {
this.a = "this property won't be assigned to the function"
是否有办法在执行时手动更改Javascript函数的执行上下文?
我是,而不是,谈论的是将this引用更改为指向另一个对象--我知道这可以用Function.prototype.call()/apply()来完成。
我指的是更改函数可以访问的变量,或者更具体地说,授予对函数定义范围之外的变量的访问权。
示例:
var func;
(function () {
func = function () {
alert(test); // no variable called "test" defined, it will
可能重复:
今天上午,我正在尝试使用javascript/jQuery,并试图捕获当前正在执行的函数的调用方名称。
因此,在下面的示例中,日志将显示runMe作为调用方,showMe显示为被调用方。
jQuery(document).ready(function($) {
function showMe() {
// should log the runMe as the caller and showMe as callee
console.log('Callee: ',arguments.callee)
console.log(
当我在函数内部时,有什么方法可以得到函数引用吗?
var arrayOfFunction = [ myFunction ];
arrayOfFunction[0]();
function myFunction() {
removeFromArray( thisFunctionReference ) // how to get function self reference here?
};
function removeFromArray(functionRef) {
index = arrayOfFunction.indexOf(functionRef);
if