Home  >  Article  >  Web Front-end  >  Can javascript release objects?

Can javascript release objects?

青灯夜游
青灯夜游Original
2021-09-07 15:35:552554browse

javascript can release objects. When an object is not referenced, the object is destroyed. JavaScript will automatically destroy all destroyed objects, that is, release the object; you can also set all references to the object to null through the "object = null;" statement. Force release of object.

Can javascript release objects?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript can automatically recycle useless storage units. When an object is not referenced, the object is destroyed, and JavaScript will automatically destroy all destroyed objects. You can force the object to be destroyed by setting all references to the object to null.

Manually release the object:

对象 = null;

When the object is not referenced by any variable, JavaScript will automatically recycle the resources occupied by the object.

Example:

var obj = {  //定义对象,被变量obj引用
    x : true,
    y : false
}
obj = null;  //设置为空,废除引用

Manually delete object method/property:

delete obj.name/obj.say();

Extended information: JS creation object (3 types Method)

1. Constructing an object

Use the new operator to call the constructor to construct an instance object.

var o = new Object();  //定义一个空对象
var a = new Array();  //定义一个空数组
var f = new Function();  //定义一个空函数

2. Object direct quantity

Using direct quantity can quickly create objects, which is also the most efficient and simple method. The specific usage is as follows:

var objectName = {
    属性名1 : 属性值1,
    属性名2 : 属性值2,
    ...
    属性名n : 属性值n
};

In object literals, the attribute name and attribute value are separated by colons. The attribute value can be any type of data, and the attribute name can be a JavaScript identifier or a string. type expression. Properties are separated by commas, and no comma is required at the end of the last property.

3. Use Object.create

Object.create is a new static method in ECMAScript 5, used to create an instance object. This method can specify the object's prototype and object properties. The specific usage is as follows:

Object.create(prototype, descriptors)

Example: Use Object.create to define an object, inherit null, and contain two enumerable attributes, size and shape, with attribute values ​​of "large" and "round" respectively.

var newObj = Object.create (null, {
    size : {  //属性名
        value : "large",  //属性值
        enumerable : true  //可以枚举
    },
    shape : {  //属性名
        value : "round",  //属性值
        enumerable : true  //可以枚举
    }
});
console.log(newObj.size);  //large
console.log(newObj.shape);  //round
console.log(Object.getPrototypeOf(newObj));  //null

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of Can javascript release objects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn