jsÔõôɾ³ý¹¤¾ßÖеÄij¸öÔªËØ
javascript ¹¤¾ßÖеÄÔªËØɾ³ýÒªÁ죺ʹÓà delete ÔËËã·û£ºdelete object[property]ʹÓà object.defineproperty()£ºobject.defineproperty(object, property, { value: undefined })ÔÚɾ³ý²»¿Éö¾ÙÊôÐÔ»ò·ûºÅʱʹÓà delete ÔËËã·û£¬ÔÚɾ³ý¿Éö¾ÙÊôÐÔµ«±£´æÆäÊôÐÔֵʱʹÓà object.defineproperty()¡£
ɾ³ý JavaScript ¹¤¾ßÖÐÔªËØ
Òª´Ó JavaScript ¹¤¾ßÖÐɾ³ýÒ»¸öÔªËØ£¬¿ÉÒÔʹÓÃÒÔÏÂÒªÁ죺
ʹÓà delete ÔËËã·û
delete ÔËËã·ûÓÃÓÚɾ³ý¹¤¾ßµÄÊôÐÔ¡£ËüµÄÓ﷨Ϊ£º
delete object[property]
µÇ¼ºó¸´ÖÆ
ÀýÈ磺
const obj = { name: 'John', age: 30 }; delete obj.age;
µÇ¼ºó¸´ÖÆ
¸Ã²Ù×÷½«´Ó obj ¹¤¾ßÖÐɾ³ý age ÊôÐÔ¡£
ʹÓà Object.defineProperty()
Object.defineProperty() ÒªÁ컹¿ÉÓÃÓÚɾ³ý¹¤¾ßÊôÐÔ¡£ËüµÄÓ﷨Ϊ£º
Object.defineProperty(object, property, { configurable: true, enumerable: true, writable: true, value: undefined });
µÇ¼ºó¸´ÖÆ
ÀýÈ磺
const obj = { name: 'John', age: 30 }; Object.defineProperty(obj, 'age', { value: undefined });
µÇ¼ºó¸´ÖÆ
¸Ã²Ù×÷½«´Ó obj ¹¤¾ßÖÐɾ³ý age ÊôÐÔ¡£
ºÎʱʹÓà delete ÔËËã·û£¬ºÎʱʹÓà Object.defineProperty()
ʹÓà delete ÔËËã·û£ºµ±ÄúÏ£Íûɾ³ý²»¿Éö¾ÙÊôÐÔ»ò·ûºÅ£¨symbol£©Ê±¡£
ʹÓà Object.defineProperty()£ºµ±ÄúÏ£Íûɾ³ý¿Éö¾ÙÊôÐÔ²¢ÇÒ²»Ï£ÍûÍêÈ«ÒƳý¸ÃÊôÐÔ£¨Ê¹Æä±äΪ undefined£©Ê±¡£
ÒÔÉϾÍÊÇjsÔõôɾ³ý¹¤¾ßÖеÄij¸öÔªËصÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡