0

 var workshop = {
      teacher: "Kyle",
      ask: function (questioin) {   // not shorthand
        console.log(this.teacher, questioin);
      },
    };
    
new (workshop.ask.bind(workshop)); //"no problem runs fine."

but the below cause TypeError: workshop.ask.bind(...) is not a constructor

 var workshop = {
      teacher: "Kyle",
      ask(questioin) {   //  shorthand
        console.log(this.teacher, questioin);
      },
    };
    
new (workshop.ask.bind(workshop)); // "this will give typeError";
pilchard
  • 12,414
  • 5
  • 11
  • 23
  • 3
    It's simply defined by the language: Method definitions cannot be constructors. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#method_definitions_are_not_constructable – Barmar Apr 10 '23 at 15:48
  • 2
    Even if it worked, using the `new` operator together with `bind` doesn't make any sense. The whole point of `bind` (at least when only using the first argument, which is the most common use) is to set the function's `this` to a particular object. Yet when using `new` you want the newly-constructed object to be the `this`. – Robin Zigmond Apr 10 '23 at 16:00
  • couldn't agree more @RobinZigmond, but i was reading YDNKJ to observe the 'this' binding precedence. – Amit Dhaterwal Apr 10 '23 at 16:08
  • This is not related to `bind`, other than that `bind` only creates a constructable function if it is called on a constructable function. As for why the method definition syntax does not create a constructor, see the duplicates. – Bergi Apr 10 '23 at 16:28
  • `ask` is a method. It should be **called** on `workshop`, as a method, not be used with `new` as a constructor. – Bergi Apr 10 '23 at 16:29

0 Answers0