Search code examples
javascriptconstructorpaperjs

Why can't I define Symbol as a constructor?


This is a snippet of my Javascript (paperjs) code:

var symbol = new Symbol(path);

it gives a warning - "Do not use Symbol as a constructor"

Is there anything else I could do here, to get rid of that warning?


Solution

  • From the Documentation

    The following syntax with the new operator will throw a TypeError:

    var sym = new Symbol(); // TypeError

    This prevents authors from creating an explicit Symbol wrapper object instead of a new symbol value and might be surprising as creating explicit wrapper objects around primitive data types is generally possible (for example, new Boolean, new String and new Number).

    Just use Symbol as a function

    var symbol = Symbol(4);
    
    console.log(typeof symbol); // outputs 'symbol'