Is there a reason for the Object.freeze function?

I understand the recursion point over the deep object to make shallow Object.freezefor each child property. What is the meaning of freezing the value of a function object? The link is already frozen due to shallow freezing at a higher level - is it possible to directly change the value of the function object?

Example:

// Library Function [deepFreeze source](https://github.com/substack/deep-freeze/blob/master/index.js)
function deepFreeze (o) {
  Object.freeze(o); // shallow freeze the top level
  Object.getOwnPropertyNames(o).forEach(function (prop) {
    if o[prop] != null // no point freezing null or undefined
    && (typeof o[prop] === "object" || typeof o[prop] === "function") {
      deepFreeze(o[prop]);
    }
  });
  return o;
};

// Usage
var x = {
  y: [1,2,3],
  z: function(){}
};
deepFreeze(x);

Just trying to understand whether there is something fundamental that I'm here I do not understand, or if it simply protects the mutation function object, for example: x.z.foo = 3.

+4
source share
1 answer

In Javascript, functions are objects. I knew that.

( prototype) :

var x = function(foo){}
Object.getOwnPropertyNames(x)
// ["length", "name", "arguments", "caller", "prototype"]
x.length; // 1, because there is one argument
x.length = 2;
x.length; // still 1.

, , :

x.foo = 3;
x.foo; // 3

, .

, , - :

, , -, .

, , , , . , .

: , , , , - .

+2

Source: https://habr.com/ru/post/1650094/


All Articles