@seungha/

fake-array

ES6

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.07982204191916553","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"LyoqCiAqIOyasOy4oeydmCBSRVBM7J2EIO2Gte2VtCBgZmFrZUFycmDsl5Ag7KCA7J6l65CY7Ja0IOyeiOuKlCDqsJ3ssrTrpbwg7KGw7J6R7ZW067O07IS47JqULgogKiDrp4jsuZgg67Cw7Je07J24IOqyg+yymOufvCDrs7TsnbTsp4Drp4wsIOyLpOygnOuhnCDrsLDsl7TsnYAg7JWE64uZ64uI64ukLgogKi8KCi8vIEFycmF5LnByb3RvdHlwZSA9PT0gb2JqLl9fcHJvdG9fXyDsmrDtmowKLy8gT2JqZWN0LmRlZmluZVByb3BlcnRpZXMoQXJyYXkucHJvdG90eXBlLCB7Ci8vICAgLy8gYE9iamVjdC5wcm90b3R5cGUudG9TdHJpbmcuY2FsbCgpYCDsmrDtmowKLy8gICBbU3ltYm9sLnRvU3RyaW5nVGFnXTogewovLyAgICAgZ2V0OiAoKSA9PiAnQXJyYXknLAovLyAgIH0sCi8vICAgLy8gYC4uLmAg7Jew7IKw7J6QIOyCrOyaqSDqsIDriqXtlZjqsowKLy8gICBbU3ltYm9sLmlzQ29uY2F0U3ByZWFkYWJsZV06IHsKLy8gICAgIHZhbHVlOiB0cnVlCi8vICAgfQovLyB9KTsKCmNvbnN0IGZha2VBcnIgPSBPYmplY3QuY3JlYXRlKAogIEFycmF5LnByb3RvdHlwZSwKICB7CiAgICBsZW5ndGg6IHsKICAgICAgdmFsdWU6IDAsCiAgICAgIHdyaXRhYmxlOiB0cnVlLAogICAgICBlbnVtZXJhYmxlOiBmYWxzZSwKICAgICAgY29uZmlndXJhYmxlOiBmYWxzZQogICAgfQogIH0KKTsKCmNvbnN0IHJlYWxBcnJheSA9IFtdOwoKcmVhbEFycmF5LmNvbnN0cnVjdG9yID09PSBBcnJheTsgLy8gdHJ1ZQpyZWFsQXJyYXkgaW5zdGFuY2VvZiBBcnJheTsgLy8gdHJ1ZQpPYmplY3QucHJvdG90eXBlLnRvU3RyaW5nLmNhbGwocmVhbEFycmF5KTsgLy8gW29iamVjdCBBcnJheV0KcmVhbEFycmF5Ll9fcHJvdG9fXyA9PT0gQXJyYXkucHJvdG90eXBlOyAvLyB0cnVlCkFycmF5LmlzQXJyYXkocmVhbEFycmF5KTsgLy8gdHJ1ZQoKZmFrZUFyci5jb25zdHJ1Y3RvciA9PT0gQXJyYXk7IC8vIHRydWUKZmFrZUFyciBpbnN0YW5jZW9mIEFycmF5OyAvLyB0cnVlCk9iamVjdC5wcm90b3R5cGUudG9TdHJpbmcuY2FsbChmYWtlQXJyKTsgLy8gW29iamVjdCBBcnJheV0KZmFrZUFyci5fX3Byb3RvX18gPT09IEFycmF5LnByb3RvdHlwZTsgLy8gdHJ1ZQpBcnJheS5pc0FycmF5KGZha2VBcnIpOyAvLyBmYWxzZSAoISEhKQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
 * 우측의 REPL을 통해 `fakeArr`에 저장되어 있는 객체를 조작해보세요.
 * 마치 배열인 것처럼 보이지만, 실제로 배열은 아닙니다.
 */

// Array.prototype === obj.__proto__ 우회
// Object.defineProperties(Array.prototype, {
//   // `Object.prototype.toString.call()` 우회
//   [Symbol.toStringTag]: {
//     get: () => 'Array',
//   },
//   // `...` 연산자 사용 가능하게
//   [Symbol.isConcatSpreadable]: {
//     value: true
//   }
// });

const fakeArr = Object.create(
  Array.prototype,
  {
    length: {
      value: 0,
      writable: true,
      enumerable: false,
      configurable: false
    }
  }
);

const realArray = [];

realArray.constructor === Array; // true
realArray instanceof Array; // true
Object.prototype.toString.call(realArray); // [object Array]
realArray.__proto__ === Array.prototype; // true
Array.isArray(realArray); // true

fakeArr.constructor === Array; // true
fakeArr instanceof Array; // true
Object.prototype.toString.call(fakeArr); // [object Array]
fakeArr.__proto__ === Array.prototype; // true
Array.isArray(fakeArr); // false (!!!)
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie