search

Home  >  Q&A  >  body text

javascript - js construction object problem

function Person {}
Person.prototype.test = function() {
} 

new Person()

new Person

These two writing methods have the same effect. Why does not adding parentheses have the same effect as the previous one?

習慣沉默習慣沉默2748 days ago518

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-05-19 10:33:03

    new constructor[([arguments])]

    When the code new Person(...) is executed:

    A new object is created. It inherits from Person.prototype.
    Constructor Person is executed. When executing, the corresponding parameters will be passed in, and the context (this) will be designated as this new instance.

    new Person is equivalent to new Person(), and can only be used without passing any parameters.


    reply
    0
  • 迷茫

    迷茫2017-05-19 10:33:03

    Is there any problem? By default, no value is passed when parentheses are not added

    function Person(val){this.val=val};//类似这种传值的就得加

    And the priorities of adding () and not adding are also different

    reply
    0
  • Cancelreply