2

有人可以看看这个并帮助我理解为什么prototype.placing 不是控制台日志记录吗?谢谢您的帮助:

(function() {
   function Rider(name) {
       this.name = name;
       this.show = function(showName) {
           console.log(this.name + " rode in the " + showName);
       };
   }

    var riderOne = new Rider("Billy Bobb");
    riderOne.show("Summer Show");

    Rider.prototype.placing = function(place) {
        console.log(this.rider + " ended up in " + place + " at " +
                this.showName);
    }
})();
4

1 回答 1

3

看看这里,它正在工作:http: //jsfiddle.net/D3BKz/1/

代码:

(function() {
   function Rider(name) {
       this.name = name;
       this.showName = "";
       this.show = function(showName) {
           this.showName = showName;
           console.log(this.name + " rode in the " + showName);
       };
   }
    Rider.prototype.placing = function(place) {
        console.log(this.name + " ended up in " + place + " at " +
                this.showName);
    }
    var riderOne = new Rider("Billy Bobb");
    riderOne.show("Summer Show");
    riderOne.placing("1st");

})();

我改变了这个:

Rider.prototype.placing = function(place) {
    console.log(this.rider+ " ended up in " + place + " at " +
            this.showName);
}

对此:

Rider.prototype.placing = function(place) {
    console.log(this.name + " ended up in " + place + " at " +
            this.showName);
}

然后我用

riderOne.placing("1st");

这是控制台输出:

Billy Bobb rode in the Summer Show
Billy Bobb ended up in 1st at Summer Show 

编辑:

正如有人指出的那样, showName 从未在 Rider 类中定义。我更新了代码,将 showName 属性添加到类中。

于 2013-06-01T17:00:53.423 回答