0

I am copying functions from one object to another. The problem is that while they are anonymous in the source object, they have a name in the target object:

var o1 = {
  a: function() {
    alert("Hello World");
  },
  b: 123,
  c: "Some string"
}

var o2 = {};

for (var key in o1) {
  if ("function" == typeof o1[key]) {
    o2[key] = o1[key];
  }
}

console.log(o2.a); //output: function o1.a()

The two functions do not seem to be connected in some way, but this is very irritating at least. Also, Firefox Developer Edition knows where the function came from if I log it in the console and click the name. And I don't know whether this might have some other bad influence somehow.

So, if it is possible to copy a function and keep it anonymous, that would be great!

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
jaySon
  • 795
  • 2
  • 7
  • 20
  • This goes even further inside of functions. Say, this code snipped above is wrapped inside a function `foo`, then the console output would be `function foo/o1.a()`. – jaySon Jul 12 '16 at 22:16
  • 1
    I just tested in Chrome and Firefox. Neither version is adding a name to the function when you put it in the other object. And it will always know where the source function was because you're not copying it, you're creating a reference. Regardless, why are you trying to keep it anonymous? – Mike Cluck Jul 12 '16 at 22:16
  • copy by reference... – epascarello Jul 12 '16 at 22:17
  • @epascarello If you want to be pedantic ;) The point is that they refer to the same exact function. – Mike Cluck Jul 12 '16 at 22:19
  • http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – epascarello Jul 12 '16 at 22:21
  • If referencing was the case, wouldn't that mean that if I delete the original `o1.a` that I wouldn't be able to invoke `o2.a`? Because that's not the case for me. I can still invoke `o2.a` even after I deleted `o1.a`. – jaySon Jul 12 '16 at 22:21
  • 1
    You'd be deleting `o1.a`s reference to the function, not the function itself. You can test that they are the same function with `o1.a === o2.a // => true`. – T. B. Jul 12 '16 at 22:24
  • I understand. But why is it that `o2.a.name` executed in Firebug Developer Edition and Chrome says "a", while in normal Firefox with Firebug, it says "(an empty string)" and in IE it says "undefined"? Same code, but different results, this is confusing. – jaySon Jul 12 '16 at 22:33
  • @jaySon—because consoles are entirely implementation dependent, they're trying to be helpful. Different development teams have different ideas on what that is. – RobG Jul 12 '16 at 23:07
  • @RobG According to MDN, the [`Function.name`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Function/name) property is yet to be implemented in most JS engines. Chrome and Firefox support it already. Otherwise it would be rather odd to access a standardized proper JavaScript value via console and get different results on different browsers (having said that I do belive that firebug has its own engine, because it supported the `let` command before the engine of Firefox ESR did). – jaySon Jul 13 '16 at 09:03

1 Answers1

1

You can use Object.assign().

var o1 = {
  a: function() {
    alert("Hello World");
  },
  b: 123,
  c: "Some string"
}

var o2 = Object.assign({}, o1);

For manipulate independents objects.

Look this too: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Julian Porras
  • 95
  • 1
  • 7
  • You're not really copying though, you're just copying references. If you do this, then `o1.a === o2.a`. He probably needs to use Function.bind(o2). – T. B. Jul 12 '16 at 22:23
  • Well, this is the proper way to "copy" an object (as an alternative for a for-in-loop), but the result is the same. – jaySon Jul 12 '16 at 22:38