js-study-part4-task3

by tigeral

HTML

<!--
Написать ф-цию, которая будет выбрасывать exception, если ее вызвали не как конструктор, т.е.  - без new.
-->

JavaScript

var myConstructor = function MyConstructor () {
  if (Object.getPrototypeOf(this).constructor.name !== 'MyConstructor') {
    throw new TypeError('This function is a constructor and should not be called as a regular function');
  }
  console.log('success');
};

new myConstructor();
try {
  myConstructor();
} catch (e) {
  console.log('exception raised');
}