Javascript object creation using literals vs custom constructor functions

前端 未结 3 793
天命终不由人
天命终不由人 2021-01-07 02:01

I understand that there are multiple ways to create an object in javascript and I have been reading that object literal syntax is generally preferred. (Correct?)

Wha

3条回答
  •  [愿得一人]
    2021-01-07 02:12

    The discussion usually is about to prefer

    var myObject = {};
    

    over

    var myObject = new Object();
    

    If you however create your own constructor functions you are perfectly allowed to instantiate them with the new keyword, nothing controversial there.

提交回复
热议问题