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

420 Views Asked by At

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);
2

There are 2 best solutions below

1
Paul On BEST ANSWER

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.

3
M. Fawad Surosh On

Use of "use strict"; at the top of page worked for me although this approach is usually used for JavaScript development. I was trying to use one of the node.js packages which didn't worked.