Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Can javascript release the object?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "whether javascript can release objects". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

JavaScript can automatically reclaim useless storage units, when an object is not referenced, the object is abolished, and JavaScript automatically destroys all deprecated objects. By setting all references to an object to null, you can force the object to be abolished.

Release the object manually:

Object = null

When an object is not referenced by any variable, JavaScript automatically reclaims the resources occupied by the object.

Example:

Var obj = {/ / defines the object, which is referenced by the variable obj x: true, y: false} obj = null; / / is set to null, abolishing the reference

Manually delete object methods / properties:

Delete obj.name/obj.say ()

Extended material: JS creates objects (3 ways)

1. Construct the object

You can construct an instance object by calling the constructor using the new operator.

Var o = new Object (); / / define an empty object var a = new Array (); / / define an empty array var f = new Function (); / / define an empty function

2. Direct quantity of object

Using direct quantities can quickly create objects, and it is also the most efficient and easiest way. The specific usage is as follows:

Var objectName = {attribute name 1: attribute value 1, attribute name 2: attribute value 2,. Attribute name n: attribute value n}

In object literals, attribute names and attribute values are separated by colons. Attribute values can be any type of data, and attribute names can be JavaScript identifiers or string expressions. Attributes are separated by commas between attributes, and the end of the last attribute does not need a comma.

3. Use Object.create

Object.create is a new static method added to ECMAScript 5 to create an instance object. This method specifies the prototype and object properties of the object. The specific usage is as follows:

Object.create (prototype, descriptors)

Example: use Object.create to define an object that inherits null and contains two enumerable properties size and shape with property values of "large" and "round", respectively.

Var newObj = Object.create (null, {size: {/ / attribute name value: "large", / / attribute value enumerable: true / / can enumerate}, shape: {/ / attribute name value: "round", / / attribute value enumerable: true / / can enumerate}}); console.log (newObj.size); / / largeconsole.log (newObj.shape) / / roundconsole.log (Object.getPrototypeOf (newObj)); / / null, "can javascript release objects"? that's all. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report