如何在javascript中调用通过原型创建的方法?

如何在javascript中调用通过原型创建的方法?,javascript,object,prototypal-inheritance,Javascript,Object,Prototypal Inheritance,我得到一个未捕获的类型错误:问题1。它不是一个函数 function Question(){ this.question = []; } function Push(){ } Push.prototype.pushIt = function(array,text){ return array.push(text); } Push.prototype = Object.create(Question.prototype); var question1 = new Question(

我得到一个未捕获的类型错误:问题1。它不是一个函数

function Question(){
  this.question = [];
}

function Push(){
}

Push.prototype.pushIt = function(array,text){
  return array.push(text);
}

Push.prototype = Object.create(Question.prototype);

var question1 = new Question();
question1.pushIt(this.question,"is 1 = 1 ?");// error

我想你可能在找类似的东西

JavaScript:

function Push() {
    this.pushIt = function(array, text){
        return array.push(text);   
    }
};

function Question() {
    this.question = [];
}

Question.prototype = new Push();

var question1 = new Question();
question1.pushIt(question1.question,"is 1 = 1 ?");

console.log(question1.question); // ["is 1 = 1 ?"]
console.log(question1 instanceof Question); // true
console.log(question1 instanceof Push); // true

好啊那么问题是什么呢?您没有创建任何类型为
Push
的对象。为什么您希望
pushIt
在类型为
Question
的对象上可用?你的意图是把
Push
变成
问题的超类吗?
——在这种情况下,你的意图是颠倒过来的。我想你误解了
原型和
对象。在这里创建
工作<代码>推送
肯定不在问题1的继承链中。