Preventing properties from being added to a JavaScript object

George

I basically just want to know if there's some standardized way to prevent methods/properties from being added to a JavaScript object?

As a c++ programmer it seems like you could end up with a lot of headaches trying to debug JavaScript code when it lets you add properties to an object on the fly, especially in large code bases.

Alexander O'Mara

There is sort-of, the ES5.1 Object.freeze function can prevent new properties from being added, as well as prevent existing properties from changing.

Working Example:

var o = {
  a: 123
};
o.b = 456;

// Freeze the object.
Object.freeze(o);

// Log the object.
console.log(o);

// These do nothing, properties do not change and are not added.
o.b = 0;
o.c = 789;

// Log the object.
console.log(o);

That being said, JavaScript is a super dynamic language, so doing this extensively might be considered an anti-pattern.

If freeze is too limiting, other options include Object.preventExtensions, and Object.seal, depending on what you wish to prevent.

Alternately ES6 Proxy objects can offer more control over which properties can be added or modified.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Preventing application from being closed when a file is open in Python

来自分类Dev

Why is a duplicate object being added to my Python set when it shouldn't be?

来自分类Dev

Preventing multiple clicks from firing

来自分类Dev

How to check what prevents an object from being eligble for garbage collection?

来自分类Dev

Pass object from Javascript to MVC Controller

来自分类Dev

accessing the parent object from the child object property in javascript

来自分类Dev

Release (drop) object being dragged

来自分类Dev

没有从原型中取消引用Object.create和properties指针的Javascript使用

来自分类Dev

How can I prevent a PDF file from being downloaded or printed with PHP or JavaScript?

来自分类Dev

Angular filter a object by its properties

来自分类Dev

Unable to add properties to js object

来自分类Dev

PHP set object properties dynamically

来自分类Dev

Pull Properties from Class

来自分类Dev

JavaScript返回[object,Object]

来自分类Dev

How to use a custom binding to animate elements being added using "foreach" in Knockout.js

来自分类Dev

ECMA Script 6 Arrow functions as object properties

来自分类Dev

Error in fsproj file prevents it from being opened

来自分类Dev

返回“ [object Object] NaN”-Javascript

来自分类Dev

JavaScript()。Serializer()获取[object,object]

来自分类Dev

JavaScript-返回[object Object]

来自分类Dev

Where to store properties from properties file in java webapp

来自分类Dev

JavaScript中的indexOf(object)

来自分类Dev

object is not a function on stripe javascript

来自分类Dev

Javascript Object.values

来自分类Dev

JavaScript中的indexOf(object)

来自分类Dev

javascript Object.create()

来自分类Dev

Javascript Object-MapAreas

来自分类Dev

查看[object HTMLInputElement]-JavaScript

来自分类Dev

Get document properties from PDF in iTextSharp

Related 相关文章

  1. 1

    Preventing application from being closed when a file is open in Python

  2. 2

    Why is a duplicate object being added to my Python set when it shouldn't be?

  3. 3

    Preventing multiple clicks from firing

  4. 4

    How to check what prevents an object from being eligble for garbage collection?

  5. 5

    Pass object from Javascript to MVC Controller

  6. 6

    accessing the parent object from the child object property in javascript

  7. 7

    Release (drop) object being dragged

  8. 8

    没有从原型中取消引用Object.create和properties指针的Javascript使用

  9. 9

    How can I prevent a PDF file from being downloaded or printed with PHP or JavaScript?

  10. 10

    Angular filter a object by its properties

  11. 11

    Unable to add properties to js object

  12. 12

    PHP set object properties dynamically

  13. 13

    Pull Properties from Class

  14. 14

    JavaScript返回[object,Object]

  15. 15

    How to use a custom binding to animate elements being added using "foreach" in Knockout.js

  16. 16

    ECMA Script 6 Arrow functions as object properties

  17. 17

    Error in fsproj file prevents it from being opened

  18. 18

    返回“ [object Object] NaN”-Javascript

  19. 19

    JavaScript()。Serializer()获取[object,object]

  20. 20

    JavaScript-返回[object Object]

  21. 21

    Where to store properties from properties file in java webapp

  22. 22

    JavaScript中的indexOf(object)

  23. 23

    object is not a function on stripe javascript

  24. 24

    Javascript Object.values

  25. 25

    JavaScript中的indexOf(object)

  26. 26

    javascript Object.create()

  27. 27

    Javascript Object-MapAreas

  28. 28

    查看[object HTMLInputElement]-JavaScript

  29. 29

    Get document properties from PDF in iTextSharp

热门标签

归档