使用空函数作为继承的构造函数会弄乱javascript中的instanceof吗?

使用空函数作为继承的构造函数会弄乱javascript中的instanceof吗?,javascript,class,inheritance,constructor,prototype,Javascript,Class,Inheritance,Constructor,Prototype,我正在尝试创建一个简单的助手函数来自动化javascript继承。工作原理如下: var myClass = makeClass({ inherit: SomeSuperClass, //optional, obviously constructor: function() {} // would like for this to be optional anotherMethod: function(){} // just gets added to the prototyp

我正在尝试创建一个简单的助手函数来自动化javascript继承。工作原理如下:

var myClass = makeClass({
   inherit: SomeSuperClass, //optional, obviously
   constructor: function() {} // would like for this to be optional
   anotherMethod: function(){} // just gets added to the prototype chain.
   // etc
});
我所有的东西都工作得很好,而且非常轻,但是我遇到了一个bug,它告诉我我不太明白我在做什么。从这个美妙的答案开始,我有以下几点:

function makeClass(properties) {

   // If the user doesn't supply a constructor, give them a generic function
   if ( ! properties.constructor ){
       properties.constructor = function(){};
   }


    if (properties.inherit) {
        properties.constructor.prototype = Object.create(properties.inherit.prototype);
        properties.constructor.prototype.constructor =  properties.constructor;
    }

    return properties.constructor;

    // Plus a simple loop to add the remaining methods given in properties to the prototype chain. Not important here
}
现在开始实施。这和预期的一样有效

var Food = makeClass({
    constructor: function(){}
});
var Bread = makeClass({
    inherit: Food,
    constructor: function(){}
});
var Sushi = makeClass({
    inherit: Food,
    constructor: function(){}
});

var bread = new Bread();
var sushi = new Sushi();

console.log(sushi instanceof Bread);  // false
console.log(bread instanceof Sushi);  // false
console.log(sushi.constructor);       // [Function: Sushi]
console.log(bread.constructor);       // [Function: Bread]
console.log(sushi instanceof Food);   // true
console.log(bread instanceof Food);   // true
console.log(sushi instanceof Sushi);  // true
console.log(bread instanceof Bread);  // true
我的问题在于面包或寿司都不能提供构造器。如果Bread不提供构造函数,因此使用在makeClass()中创建的泛型函数,则:

如果Sushi不提供构造函数,那么bread将成为Sushi的一个实例。为什么会这样?我可以理解它们的计算结果是真是假,但是为什么删除Bread的构造函数会影响sushi实例呢?我可以想象我的问题是,如果properties.constructor是空的,则将空函数分配给它,但我不知道如何执行此操作

如果我想做的是不可能的,或者不是最佳实践,我也想知道这一点。尽管如此,我似乎遗漏了一些基本的东西。我在SO和谷歌搜索了几个小时,似乎找不到同样的问题

谢谢

如果Bread不提供构造函数,那么使用makeClass()中创建的泛型函数

不完全是<代码>属性。在您的条件下测试的构造函数,将()始终具有一个值:它确实继承自。这将使您的
makeClass()
调用返回
Object
函数,并且
sushi
确实是Object的
实例

因此,这是我们需要使用的情况之一:


哇,太棒了。非常感谢。
(sushi instanceof Bread) become **true**
…
// If the user doesn't supply a constructor, give them a generic function
if ( !properties.hasOwnProperty("constructor") ){
    properties.constructor = function(){};
}
…