Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace __proto__ with Object.getPrototypeOf #46

Open
Xuhv opened this issue Aug 2, 2024 · 1 comment
Open

replace __proto__ with Object.getPrototypeOf #46

Xuhv opened this issue Aug 2, 2024 · 1 comment

Comments

@Xuhv
Copy link

Xuhv commented Aug 2, 2024

When I use zag ui with deno, some components doesn't work because deno doesn't implement __proto__.

Object.getPrototypeOf also has good compatibility, so I think replacing __proto__ is ok.

chakra-ui/zag#1717

@Nevro
Copy link

Nevro commented Aug 23, 2024

To not waste too many words...

import { klona as klonaLite } from 'klona/lite';
const obj = Object.create(null, Object.getOwnPropertyDescriptors({
  key1: 'val1', 
  key2: 'val2' 
}));
assert.deepStrictEqual(klonaLite(obj), obj);
/*
AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected
+ {
- [Object: null prototype] {
    key1: 'val1',
    key2: 'val2'
  }
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: { key1: 'val1', key2: 'val2' },
  expected: [Object: null prototype] { key1: 'val1', key2: 'val2' },
  operator: 'deepStrictEqual'
}*/
import { klona as klonaLite } from 'klona/lite';
const obj = {
  key1: 'val1',
  key2: 'val2',
  __proto__: {
    key3: 'val3-parent',
    key4: 'val4-parent',
  }
};
assert.deepStrictEqual(klonaLite(obj), obj);
/*
AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected
  {
    key1: 'val1',
    key2: 'val2',
+   key3: 'val3-parent',
+   key4: 'val4-parent'
  }
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: {
    key1: 'val1',
    key2: 'val2',
    key3: 'val3-parent',
    key4: 'val4-parent'
  },
  expected: { key1: 'val1', key2: 'val2' },
  operator: 'deepStrictEqual'
}*/

Object.prototype.__proto__ is an accessor property with attribute Enumerable: false

The for...in statement iterates over all enumerable string properties of an object including inherited enumerable properties.

patch for lite/basic:

...
var k, proto, tmp, str=Object.prototype.toString.call(x);

if (str === '[object Object]') {
    if (x.constructor === undefined) {
        tmp = Object.create(null);
        for (k in x)
            tmp[k] = klona(x[k]);
    } else {
        proto = x.__proto__ || Object.getPrototypeOf(x);
        tmp = (proto !== Object.prototype)
            ? Object.create(proto)
            : {};
        for (k in x)
            if (Object.prototype.hasOwnProperty.call(x, k) === true)
                tmp[k] = klona(x[k]);
    }
    return tmp;
}
...

Now implement this to full version..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants