猿问

js原型小问题

function Tt(){

    this.name = "hello";

    this.age = 88;

    if(typeof this.say != "function"){

        Tt.prototype = {

            constructor: Tt,

            say: function(){

                return this.name;

            }

        };

    }

}

var t = new Tt();

**console.log(t.say());   // Uncaught TypeError: Object #<Tt> has no method 'say'  这里为什么访问不到方法???**



// 如果这么做

function Tt(){

    this.name = "hello";

    this.age = 88;

    if(typeof this.say != "function"){

        Tt.prototype.say = function(){

                return this.name;

            }

    }

}

var t = new Tt();

**console.log(t.say()); // 这样就没有问题!**


慕后森
浏览 492回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答