---
schema_version: "1"
id: a0b563cb3c87c3849668b2bbdae18879cd60638089839e63872d148b5c148908
title: Cloned Objects Behavior Differently in LWS Compared to Lightning Locker
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/lwc/lightning-components-security/guides/lws/lws-clone-objects.md
canonical_url: https://developer.salesforce.com/docs/platform/lightning-components-security/guide/lws-clone-objects.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: c0c94f73cfdee1d02888ec9b8459d175700a8f4c7df8c8fd4a1b05ee06e5b5b2
products:
  - Security for Lightning Components
  - lwc
---
# Cloned Objects Behavior Differently in LWS Compared to Lightning Locker

If you clone objects using `JSON.stringify(o)`, the behavior in Lightning Web Security (LWS) is different compared to Lightning Locker. Your code might rely on a non-standard behavior of Lightning Locker that doesn’t occur with LWS.

In Lightning Locker, platform objects are replaced with a secure wrapper. To illustrate the behavior, let’s look at the `Event` object. `Event` objects are exposed as `SecureEvent` objects. `SecureEvent` objects are new objects in which all the [inherited and direct properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty#direct_vs._inherited_properties) of the original `Event` object become direct properties in the new object. Direct properties are called "own properties". The conversion to own properties means that the inherited property `Event.prototype.detail` becomes an own property of the `SecureEvent` instance.

`JSON.stringify(o)` only operates on own properties, but if you use it on an `Event` object when Lightning Locker is enabled, all the properties are included because `SecureEvent` converts them to own properties. Under Lightning Locker, you can “clone” received objects by using `JSON.parse(JSON.stringify(o))`.

LWS doesn’t use secure wrappers, and objects typically behave as they would on the browser platform. For the `Event` object under LWS, `Event.prototype.detail` isn’t included in the properties serialized by `JSON.stringify(o)`, because that property is inherited and is not an own property. Therefore, techniques that rely on all properties being serialized won’t work the same in LWS.

`CustomEvent` object cloning through `JSON.parse(JSON.stringify(o))` is one area where developers have run into issues while transitioning to LWS. `CustomEvent.detail` is not an own property so it’s stripped away when cloning.

For example, this code works in Lightning Locker only because wrapping by `SecureEvent` converts the `detail` property to an own property that `JSON.parse(JSON.stringify(event))` can parse.

```javascript
// This cloning technique works in Lightning Locker, but not in LWS.
// Inherited properties aren't included in JSON.stringify serialization,
// and therefore will not be in the cloned object in LWS.

const div = document.createElement("div");

function passItAlong(payload) {
  console.log(payload);
}

div.addEventListener("foo", (event) => {
  passItAlong(JSON.parse(JSON.stringify(event)));
});

const event = new CustomEvent("foo", {
  detail: {
    a: 1,
    b: 2,
  },
});

div.dispatchEvent(event);
```

To pass the value of the `detail` property of an `Event` object, we recommend destructuring the property from the object and reassigning into a new object.

```javascript
// This cloning technique works in LWS.

const div = document.createElement("div");

function passItAlong(payload) {
  console.log(payload);
}

div.addEventListener("foo", (event) => {
  const { detail, type, ...ownProperties } = event;
  passItAlong({ detail, type, ...ownProperties });
});

const event = new CustomEvent("foo", {
  detail: {
    a: 1,
    b: 2,
  },
});

div.dispatchEvent(event);
```
