Skip to content

Commit

Permalink
Object.create
Browse files Browse the repository at this point in the history
  • Loading branch information
msn0 committed Apr 25, 2017
1 parent 98c7b3c commit f2b8381
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ npm i mdn-polyfills --save
import 'mdn-polyfills/Object.assign';
```

## [Object.create](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Polyfill)

```js
import 'mdn-polyfills/Object.create';
```

## [Array.prototype.find](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find?v=control#Polyfill)

```js
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mdn-polyfills",
"version": "2.1.2",
"version": "2.2.0",
"description": "MDN polyfills",
"scripts": {
"prepublish": "webpack",
Expand All @@ -22,7 +22,8 @@
"array.forEach",
"array.filter",
"array.includes",
"object.assign"
"object.assign",
"object.create"
],
"author": "",
"license": "MIT",
Expand Down
22 changes: 22 additions & 0 deletions src/Object.create/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default function(undefined) {
var Temp = function() {};
return function (prototype, propertiesObject) {
if(prototype !== Object(prototype)) {
throw TypeError(
'Argument must be an object, or null'
);
}
Temp.prototype = prototype || {};
var result = new Temp();
Temp.prototype = null;
if (propertiesObject !== undefined) {
Object.defineProperties(result, propertiesObject);
}

// to imitate the case of Object.create(null)
if(prototype === null) {
result.__proto__ = null;
}
return result;
};
};
5 changes: 5 additions & 0 deletions src/Object.create/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import create from './create';

if (typeof Object.create !== 'function') {
Object.create = create();
}
10 changes: 10 additions & 0 deletions src/Object.create/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import test from 'ava';
import create from './create';

test('should create object with given prototype', t => {
const foo = { foo: 'foo' };

const object = create()(foo);

t.deepEqual(object.__proto__, { foo: 'foo' });
});
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const webpack = require('webpack');
module.exports = {
entry: {
"Object.assign": "./src/Object.assign/index.js",
"Object.create": "./src/Object.create/index.js",
"Array.prototype.find": "./src/Array.prototype.find/index.js",
"Array.prototype.from": "./src/Array.prototype.from/index.js",
"Array.prototype.filter": "./src/Array.prototype.filter/index.js",
Expand Down

0 comments on commit f2b8381

Please sign in to comment.