Search code examples
node.jsnode.js-streamnode.js-connectnode.js-clientnode.js-domains

require('use-strict') doesn't work for me


here, I am attempting to set value for read-only property but I am not getting any error:

HERE IS MY CODE:

require('use-strict');

function Employee(firstname) {
    var _firstname = firstname;

    Object.defineProperty(this, 'firstName', {
        get: function () { return _firstname },
        //set: function (value) { _firstname = value }
    });
}

var employee = new Employee('Fawad');

employee.firstName = 'Yasir'; //Attempting to set a value for read-only property.

console.log(employee.firstName);

Solution

  • From the documentation for the use-strict package:

    The implementation works by patching Node's internal module.wrapper array, and then freezing it, so that further modifications are not possible.

    Also, this means that the current module will not be affected. You should still "use strict" in the module that does require('use-strict'). This module applies strictness to all future modules loaded by your program.