Setting javascript prototype function within object class declaration

前端 未结 5 641
既然无缘
既然无缘 2020-12-04 20:08

Normally, I\'ve seen prototype functions declared outside the class definition, like this:

function Container(param) {
    this.member = param;
}
Container.p         


        
5条回答
  •  悲哀的现实
    2020-12-04 20:35

    You need to put the function on each specific instance instead of the prototype, like this:

    Container = function(param) {
        this.member = param;
        var privateVar = param;
    
        this.stamp = function(string) {
            return privateVar + this.member + string;
        }
    }
    

提交回复
热议问题