使用ES6 class
语法,我想知道为什么instanceof
当有多个继承链时,运算符不适用于继承链?
(可选阅读)
instanceof
操作员如何工作?在
obj instanceof Constructor
,instanceof
操作员检查函数的'prototype'
属性Constructor
是否存在于原型链中obj
.如果它存在,请返回true
.否则,false
.
在下面的代码片段中,BTError
继承自Error
(1.)并SomeError
从BTError
(3.)扩展.
但是,正如我们可以看到从(4),该instanceof
操作结果false
为new SomeError() instanceof BTError
它在我的理解应该是true
.
class BTError extends Error {}
console.log('1.', Reflect.getPrototypeOf(BTError.prototype) === Error.prototype); // 1. true
console.log('2.', new BTError() instanceof Error); // 2. true
console.log('');
class SomeError extends BTError {} …
Run Code Online (Sandbox Code Playgroud)
unique compound
与其他unique
只有索引的此类问题不同,问题与索引有关。我也有sparse: true
索引。
我的收藏中有以下索引
[
{
"v": 2,
"key": {
"_id": 1
},
"name": "_id_",
"ns": "somedb.votes"
},
{
"v": 2,
"key": {
"answerId": 1
},
"name": "answerId_1",
"ns": "somedb.votes",
"sparse": true,
"background": true
},
{
"v": 2,
"key": {
"questionId": 1
},
"name": "questionId_1",
"ns": "somedb.votes",
"sparse": true,
"background": true
},
{
"v": 2,
"unique": true,
"key": {
"answerId": 1,
"votedBy": 1
},
"name": "answerId_1_votedBy_1",
"ns": "somedb.votes",
"sparse": true,
"background": true
},
{
"v": …
Run Code Online (Sandbox Code Playgroud) 在阅读Eric Elliot 撰写的这篇文章https://medium.com/javascript-scene/the-single-biggest-mistake-programmers-make-every-day-62366b432308时,我遇到了以下类型的对象方法定义.
var obj = {
getX() {
document.write('X');
}
}
obj.getX(); // X
Run Code Online (Sandbox Code Playgroud)
它与以下类型的定义有何不同?
var obj = {
getX: function getX() {
document.write('X');
}
}
obj.getX(); // X
Run Code Online (Sandbox Code Playgroud)
javascript ×2
class ×1
ecmascript-6 ×1
es6-class ×1
function ×1
indexing ×1
inheritance ×1
mongodb ×1
object ×1