Skip to content Skip to sidebar Skip to footer

Is There Any Difference Between Calling Function In Functionname.prototype.methodname() And Funobject.methodname()?

If I create a function constructor like this function ClassName() { } and then I write methods for this constructor like this ClassName.prototype.a = function() { console.log('A E

Solution 1:

The difference is function context

functionClassName(name) {
  this.name = name;
}

ClassName.prototype.a = function() {
  console.log("A Executed", this.name, this === obj, this === ClassName.prototype)
}

var obj = newClassName('test')
obj.a();
ClassName.prototype.a()

Post a Comment for "Is There Any Difference Between Calling Function In Functionname.prototype.methodname() And Funobject.methodname()?"