Why can't i set the 'prototype' of a function created using 'bind'?

  • A+
Category:Languages

consider the code:

function foo(something) {   this.a = something; }  var obj1 = {};  var bar = foo.bind(obj1); 

now the following statement doesn't execute:

bar.prototype.newprop = "new";//cannot execute this 

as i understood,every function has a prototype object. then why can't we execute the above statement. and bar is indeed a function as we can call it:

bar(2); console.log(obj1.a); // 2 

 


as i understood, every function has a prototype object

Well, there are exceptions to every rule :-) You found one: bound functions don't have a .prototype property because they don't need it. When you call a bound function with new, it calls the original function as a constructor, using the original's .prototype object as the prototype of the new instance.

In fact, since ES6 many functions don't have a .prototype property with an object, because they are not constructors - they cannot be called with new so they don't need it. Among those are

  • arrow functions (() => {…})
  • methods (method() { … } in object literals and classes)
  • builtin non-constructor functions (like Math.sin)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: