What is the reason to use the 'new' keyword at Derived.prototype = new Base

后端 未结 6 1744
甜味超标
甜味超标 2020-11-21 05:27

What does the following code do:

WeatherWidget.prototype = new Widget;

where Widget is a constructor, and I want to extend the

6条回答
  •  自闭症患者
    2020-11-21 05:32

    JavaScript functions are "MULTIPLE(2) PERSONALITIES"!!!

    They are regular-functions with input and output, which we call like function().

    Also they are constructors of JS-objects, when we use the new keyword. >>>BUT<<< the new created objects are NOT INSTANCES of the constructors (like the objects of classes in class-based inheritance). The new objects are instances of the object of the prototype property of the constructor.

    Then in WeatherWidget.prototype = you put the object you want to inherit its properties to the objects the constructor will create, which usually is new function() and not a function.

    JavaScript created HUGE confusion in the programming community by naming the objects created by constructors, INSTANCES of them with the instanceof keyword.
    > function f(){}
    undefined
    > new f() instanceof f
    true

提交回复
热议问题