- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
ECMAScript 规范函数 IsCallable如果其参数具有 [[Call]] 内部方法,则返回 true。它在规范中的多个地方使用,例如在 Array.prototype.toString
的定义中.
还有一个类似的规范函数IsConstructor如果其参数具有 [[Construct]] 内部方法,则返回 true。
一些 JS 对象,包括大多数内置函数,如 escape
是可调用但不可构造的。 有没有可构造但不可调用的?
请注意,用户定义类和内置类在作为普通函数调用时都会抛出 TypeError
,但根据 IsCallable 的定义仍然可以调用,这可以通过查看是否 Array 来确定.prototype.toString
将尝试使用它们作为 join
的实现:
// {} is not callable, so toString falls back to Object.prototype.toString:
console.log('null:', Array.prototype.toString.apply({join: {}}));
// WeakMap is callable (but throws TypeError):
console.log('null:', Array.prototype.toString.apply({join: WeakMap}));
// User-defined classes also callable:
console.log('null:', Array.prototype.toString.apply({join: class Foo {}}));
最佳答案
重述:对象 X 是否有可能为 IsConstructor(X)
返回 true
但为 IsCallable(X) 返回
?即,对象 X 是否可能具有 false
)[[Construct]]
内部方法但没有 [[Call]]
内部方法?
ECMAScript 规范在这一点上并没有像它应该的那样明确。
(1) 6.1.7.2 "Object Internal Methods and Internal Slots"说:
A function object is an object that supports the [[Call]] internal method. A constructor (also referred to as a constructor function) is a function object that supports the [[Construct]] internal method.
由此我们可以得出结论,如果这样的对象 X 确实 存在,那么它显然不是“函数对象”,因此也不是“构造函数”。 IE。 IsConstuctor(X)
将为不被视为“构造函数”的对象返回 true
,这很奇怪。
(2)请注意,在定义 IsConstructor
的子句中,序言说明它确定其参数是否“是具有 [[Construct]] 内部方法的函数对象”,但该算法没有明确的检查参数是否为函数对象。这表明(规范编写者认为)具有 [[Construct]] 内部方法足以保证参数是一个函数对象,即它具有 [[Call]] 内部方法。
(3)拥有 [[Construct]]
内部方法的唯一要点是在 the Construct
abstract operation 中调用它.与第 (2) 点类似,序言中说该操作“用于调用函数对象的 [[Construct]] 内部方法”,但该算法并未明确检查 F
是一个函数对象。
所以我相信答案是,虽然规范没有明确说这样的对象不存在,但它相当强烈地暗示/假设它们不存在。
更新(2018-05-22):
6.1.7.2 "Object Internal Methods and Internal Slots"现在已修改为(强调我的):
A function object is an object that supports the [[Call]] internal method. A constructor is an object that supports the [[Construct]] internal method. Every object that supports [[Construct]] must support [[Call]]; that is, every constructor must be a function object. Therefore, a constructor may also be referred to as a constructor function or constructor function object.
关于javascript - 是否存在 IsCallable 为假但 IsConstructor 为真的 JS 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50109554/
我注意到一些 PHP 框架只使用小写字母 true/false 而其他框架则使用大写字母。 这有什么区别吗?我个人更喜欢小写字母。 最佳答案 没什么区别,有些人认为 FALSE 是常量,因此使用旧的尖
在我看来,以下两种映射方式实际上没有区别。以下是基于 @MapsId javadoc 的示例: // parent entity has simple primary key @Entity publ
我想知道是否有人知道编译器如何解释以下代码: #include using namespace std; int main() { cout << (true && true || false &
我想知道为什么alert(new Boolean(false))打印 false 而不是打印对象,因为 new Boolean 应该返回对象。如果我使用 console.log(new Boolean
即使在 verify=False 时,Python 请求也会给我一个 ssl 握手失败(我知道不使用 SSL 是不可取的)。对于其他具有有效证书的站点,请求按预期工作。我正在使用2.7。 from l
TDPL 描述了 assert(false); 语句的行为。此类断言不会从发布版本中删除(与所有其他断言一样),并且实际上会立即停止程序。问题是为什么?为什么会有如此令人困惑的行为?他们可能会添加 h
如何确定文档是否存在于 Meteor 的集合中? 编辑:新代码。 mongodb 有一个 ProductName 的文档:Apples 输入产品是“苹果” var exists = Products.
我想在 R 中做一些相当复杂的事情,但我不确定从哪里开始。 我有一个看起来像这样的数据框: main_val sub_val bit_one bit_two one a 1
我正在尝试编写一个函数 long(S1,S2) 如果 S1 比 S2 长,则该函数应该为真,否则为假。到目前为止,我所拥有的是以下内容: longer(A,nil). longer(nil,B) :-
我想知道我在这里缺少什么我有一个代码可以检查图像是否基于 Canvas 是透明的。 function Trasparent(url, npc, clb) { var img = new Ima
我想在按下按钮时使其他类框架设置可见(false)。 有一个名为 DisplayScore 的类,该类获取 getContentPane().add(new ScoreInfo());来自 Score
我正在使用 TableLayout 和 TableRow 创建 6/6 网格按钮。 private Button myButton; private static final int
这个问题没有实际用途!我之所以问这个只是因为我很好奇! 在C++中,有一种方法可以通过在某处编写#define true false来将true伪造为false,然后在代码中到处的true都将被视为f
我不久前学习 java,所以也许我有一个愚蠢的问题。 我有一张表,我在其中存储了一些信息。我想在此表中检查一些信息的可用性(这将只有一行),如果是 - 从这一行中获取特定列中的信息,如果没有 - 做另
这是一个 JavaScript 问题... 我正在尝试返回完全不区分大小写的匹配项。例如,如果我在输入框中输入 "yo",我希望它返回 true,这在我当前的方法中是这样做的,但它也会返回 true
我认为,我在 JS 中写了一个直接的 if 语句,但它运行不正确。 function printLetter(LetterId) { var studentflag = $("#IsStude
我如何使用 Javascript 执行以下操作? var object function() { return { object: Return true if object
我试图让导航栏在向上滚动时向下滑动,并在单击#menu-bar 时保持固定。但是当我再次单击同一个按钮时,我不知道如何在自动隐藏时将值改回 false,就在我有两个按钮的时候。非常感谢任何帮助! $(
下面的代码 #include using namespace std; int main(){ char greeting[50] = "goodmorning everyone";
我正在使用数据源将数据填充到我的数据 GridView 中。但是,我正在尝试找到一种方法让用户能够隐藏他不想看到的列。 我可以在程序运行之前隐藏和显示列: [Browsable(false)] pub
我是一名优秀的程序员,十分优秀!