语法错误,但不能调试javascript原型

时间:2013-12-15 05:57:49

标签: javascript

// create your Animal class here
function Animal(name, numLegs){
    this.name = name;
    this.numLegs = numLegs;
}


// create the sayName method for Animal
Animal.prototype.sayName(){
    console.log("Hi my name is " + this.name);
}

// provided code to test above constructor and method
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();

不工作,找不到什么问题。除Chrome控制台之外还有哪些调试工具?有时它无法判断哪条线路有问题。

2 个答案:

答案 0 :(得分:2)

您必须创建像这样的原型方法

Animal.prototype.sayName = function(){
    console.log("Hi my name is " + this.name);
}

答案 1 :(得分:0)

试试这个:

// create your Animal class here
function Animal(name, numLegs){
    this.name = name;
    this.numLegs = numLegs;
}


Animal.prototype.sayName = function(){
    console.log("Hi my name is " + this.name);
}

var penguin = new Animal("Captain Cook", 2);
penguin.sayName();