SyntaxError:missing; 在javascript原型中的声明之前

Ari*_*rif 0 javascript prototype

(function(){

    var Person = function(name){
        this.name = name;
    };

    Person.prototype.getName(){
        return this.name;
    };

    var arif = new Person("Arif");

    console.log(arif.getName());

})();
Run Code Online (Sandbox Code Playgroud)

我收到此代码的错误如下.我的错误在哪里?

在此输入图像描述

小智 6

我想这就是你想要的:

(function(){

    var Person = function(name){
        this.name = name;
    };

    Person.prototype.getName = function(){
        return this.name;
    };

    var arif = new Person("Arif");

    console.log(arif.getName());

})();
Run Code Online (Sandbox Code Playgroud)

基本上,您需要在原型行上定义"getName",并且您在.prototype行上使用的语法试图调用它(在它定义之前).