How to freeze properties inside a contructor function in JavaScript

Abdi

I'm trying to make the properties inside a constructor function immutable, i.e. the properties should not be altered either with a dot or bracket notation. E.g. I have my constructor function:

function OnceNamedOne() {
    Object.freeze(this.firstName = 'John');
    Object.freeze(this.lastName = 'Doe');
    this.fullName = this.firstName + ' ' + this.lastName;
}

I basically want to freeze the properties and hard wire their values as in the function. So, when an instance is created:

var me = new OnceNamedOne(); and when the value I try to change the property value, it should not work - that is the following should not assign 'John' to first name: me.firstName = 'John';.

How could I do this?

adeneo

You can't freeze a property with a primitive, but you could make it non-writable

Object.defineProperty(this, 'firstname', {
  enumerable   : false,
  configurable : false,
  writable     : false,
  value        : 'John'
});

FIDDLE

For multiple properties, it's probably easier to make a convenience function

function createNonWritable(obj, key, value) {
    Object.defineProperty(obj, key, {
          enumerable   : false,
          configurable : false,
          writable     : false,
          value        : value
    });
}

function OnceNamedOne() {
    createNonWritable(this, 'firstname', 'John');
    createNonWritable(this, 'lastname', 'Doe');
    createNonWritable(this, 'fullname', this.firstname + ' ' + this.lastname);
}

FIDDLE

Or as noted in the comments, as the attributes has a default value of false you don't have to explicitly set them to that

function OnceNamedOne() {
    Object.defineProperty(this, 'firstname', {value : 'John'});
    Object.defineProperty(this, 'lastname',  {value : 'Dow'});
    Object.defineProperty(this, 'fullname',  {value : this.firstname + ' ' + this.lastname});
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to freeze properties inside a contructor function in JavaScript

From Dev

How to troubleshoot 'No parameterless contructor'

From Dev

How to call a function inside a main function in javascript

From Dev

how to define a Function inside a Function in javascript?

From Dev

how to return a function inside a function javascript/nodejs?

From Dev

How to call a function that is inside another function javascript

From Dev

How to call a function inside a main function in javascript

From Dev

How to stringify Javascript function object with properties?

From Dev

How to stringify Javascript function object with properties?

From Dev

Checking if parameter is a contructor or an instance in javascript

From Dev

Checking if parameter is a contructor or an instance in javascript

From Dev

In JavaScript, how to access object properties inside an array of object?

From Dev

How to access variables inside a function in javascript

From Dev

How to run a function inside a forloop asynchronously in javascript

From Dev

How to access property inside function constructor javascript

From Dev

How to give parameters inside function expression in Javascript?

From Dev

how to get the variable inside a function in javascript

From Dev

How to access a javascript function inside an ajax request

From Dev

How to access variables inside a function in javascript

From Dev

How to put JsBarcode inside javascript button function?

From Dev

how to call or mimic a javascript function inside a webview

From Dev

how to loop input tag inside javascript function

From Dev

How to call a Javascript function inside an webview on nativescript?

From Dev

How to call javascript function inside modal content?

From Dev

how to pass values inside javascript function along with this

From Dev

how to use a global variable inside a function in javascript?

From Dev

How to change a variable declare inside a function in javascript

From Dev

How to reference different locations inside a JavaScript function

From Dev

JavaScript: How to return an outer function inside an asynchronous inner function?

Related Related

  1. 1

    How to freeze properties inside a contructor function in JavaScript

  2. 2

    How to troubleshoot 'No parameterless contructor'

  3. 3

    How to call a function inside a main function in javascript

  4. 4

    how to define a Function inside a Function in javascript?

  5. 5

    how to return a function inside a function javascript/nodejs?

  6. 6

    How to call a function that is inside another function javascript

  7. 7

    How to call a function inside a main function in javascript

  8. 8

    How to stringify Javascript function object with properties?

  9. 9

    How to stringify Javascript function object with properties?

  10. 10

    Checking if parameter is a contructor or an instance in javascript

  11. 11

    Checking if parameter is a contructor or an instance in javascript

  12. 12

    In JavaScript, how to access object properties inside an array of object?

  13. 13

    How to access variables inside a function in javascript

  14. 14

    How to run a function inside a forloop asynchronously in javascript

  15. 15

    How to access property inside function constructor javascript

  16. 16

    How to give parameters inside function expression in Javascript?

  17. 17

    how to get the variable inside a function in javascript

  18. 18

    How to access a javascript function inside an ajax request

  19. 19

    How to access variables inside a function in javascript

  20. 20

    How to put JsBarcode inside javascript button function?

  21. 21

    how to call or mimic a javascript function inside a webview

  22. 22

    how to loop input tag inside javascript function

  23. 23

    How to call a Javascript function inside an webview on nativescript?

  24. 24

    How to call javascript function inside modal content?

  25. 25

    how to pass values inside javascript function along with this

  26. 26

    how to use a global variable inside a function in javascript?

  27. 27

    How to change a variable declare inside a function in javascript

  28. 28

    How to reference different locations inside a JavaScript function

  29. 29

    JavaScript: How to return an outer function inside an asynchronous inner function?

HotTag

Archive