Unexpected output from Object.assign

So I got a little surprised by:

Object.assign({}, Math)

Which returned an empty object. I was doing this to set up my own math module, that I could extend with my own custom functionality, so I thought it would be convenient to do:

Object.assign({}, Math, { 
  sqrt: <my-sqrt-implementation>, 
  add: <my-add-implementation >,
})

But I was surprised to find that the math entries were not included, why is that? In my console I get:

Object.assign({}, Math)
> {}

What went wrong, and how can this be fixed?

The properties of Math are not enumerable. Object.assign will copy only enumerable and own properties:

The Object.assign() method only copies enumerable and own properties from a source object to a target object. It uses [[Get]] on the source and [[Set]] on the target, so it will invoke getters and setters. Therefore it assigns properties versus just copying or defining new properties. This may make it unsuitable for merging new properties into a prototype if the merge sources contain getters. For copying property definitions, including their enumerability, into prototypes Object.getOwnPropertyDescriptor() and Object.defineProperty() should be used instead.

Thus you should follow the suggestion, and use Object.getOwnPropertyNames(Math) to get a list of own property names, then Object.defineProperty() to assign them:

const props = Object.getOwnPropertyNames(Math)
const myMath = {}
props.forEach(prop => {
    Object.defineProperty(myMath, prop, Object.getOwnPropertyDescriptor(Math, prop))
})

myMath.sin(3.14)  // 0.00159265...

Of course, it is probably better to use prototypical inheritance (Object.create) here. I provide this code because it more closely matches the behavior of Object.assign, i.e. assignment.

JavaScript Arguments Object returning unexpected output, that logging the arguments object to the console is returning an unexpected output. /Node/notes-node-app/node_modules/lodash.assign/index.js': [Object],​  Get ready for Codegeist. Hey y’all! 🎉 Codegeist is back in a big way for 2020. How big, you ask? 💰 $300K in prizes big 💰 From now until July 13, we’re looking for innovative cloud apps in two tracks: Buil

From : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

The Object.assign() method only copies enumerable and own properties from a source object to a target object

Math properties aren't enumerable or own.

Example:

How can this be fixed?

Use Object.create e.g.

var MyMath = Object.create(Math);
MyMath.sqrt = function () {
};

Object.assign(), The Object.assign() method copies all enumerable own properties from one or more source objects expected output: Object { a: 1, b: 4, c: 5 }. 😉 Material-UI also uses Object.assign, so I won't try to remove it. I'm glad that you found a solution, might be useful for other people having the same problem. I'm glad that you found a solution, might be useful for other people having the same problem.

The properties and methods under the Math object are set up as non-iterable. When Object.assign tries to copy them, they don't show up.

If you add your own property to the Math object, you'll see it is found and copied over.

Object.create, on the other hand, should do what you want.

// You shouldn't ever really do this.
Math.testProperty = true;
var myMath = Object.assign({}, Math);

console.log(myMath.testProperty);

Object.assign vs Object Spread in Node.js, The Object Rest/Spread Proposal reached stage 4 in 2018, which means it will be included in a future The Object spread operator {obj} is similar to Object.​assign() , so which one should you use? Here's the output: parserOptions: # Otherwise object spread causes 'Parsing error: Unexpected token . Use an npm module like object-assign:. Run npm install --save object-assign; Import and use object-assign instead of Object.assign; I think that the first option is better because it enables a way to use the standard method and you shouldn't import an npm package each time you want to use Object.assign, but I wanted to enumerate all possible ways to solve this problem.

React: Tools & Resources, The store is essentially a state tree where states for all objects are kept. The way we do this can often cause unexpected bugs in our projects. the Node interactive shell: node Next, let's create an array, then later assign it to another 3, 9 ] // b output > console.log(a) [ 1, 2, 3, 9 ] // a output As you can see, updating array b  Object.assign()copies the values (of all enumerable own properties) from one or more source objects to a target object. It has a signature of Object.assign(target, sources). The target object is the first parameter and is also used as the return value. Object.assign()is useful for merging objects or cloning them shallowly.

A Guide to MATLAB: For Beginners and Experienced Users, y x 1x1 1x1 126 sym object 126 sym object Grand total is 183 elements using 2968 bytes For example, type sin('pi') to see how unexpected output can result from supplying In MATLAB, you use the equal sign to assign values to a variable. This is a task for collecting some confusing problems during developing under IE, mostly when we are really get confused with IE8!! To avoid struggling with IE, it's smart to do this as early as I can.

Intro To Oop And C++ (Isrd), To link your object file, select Link Exe File from the Compile menu or press These errors include division by 0, stack overflow and the null pointer assignment​. is an error which causes a program to produce incorrect or unexpected output​. And the resultant output is this: The first 2 lines of output make sense but the 3rd one does not. I expected it to output contents of original directory variable: /media/sf_code/scripts but it is just printing the name of the variable. Why doesn't it display the contents of the originalDirectory variable after the colon in that string?

Comments
  • I believe the Math properties are own (e.g., Math.hasOwnProperty('abs') is true). You are correct that they are not enumerable.
  • If this is the case, then something like var myMath = Object.assign(Object.create(Math), {...myMethods}) should work then. But its not working in my console.