1

In livescript, we can use ^^ to clone an object.

For example,

consloe.log (^^{a:1})

will be compiled to

// Generated by LiveScript 1.2.0
(function(){
  console.log(clone$({
    a: 1
  }));
  function clone$(it){
    function fun(){} fun.prototype = it;
    return new fun;
  }
}).call(this);

However, these codes work successfully in browser but not in node.js.

  • In browser, it prints fun {a: 1} in console.
  • In node.js, it shows nothing.

What's the reason?

nalply
  • 26,770
  • 15
  • 78
  • 101
waitingkuo
  • 89,478
  • 28
  • 112
  • 118
  • 1
    I have tried to test this in browser and node.js, every time I get an empty object {} with _proto_ set to {a: 1} . Even by looking at the generated script and livescript website: ^^ clones only the prototype not the hasOwnProperty(s). You may want to review <<< operator in livescript which may perform your desired task. – Nitin... Jan 06 '14 at 10:20
  • @Nitin... post that as an answer :). – Ven Jan 13 '14 at 09:04
  • Livescript doesn't compile that to `Object.create`? – Bergi Jan 13 '14 at 19:07
  • possible duplicate of [Why does console.log() only print member fields?](http://stackoverflow.com/questions/21104397/why-does-console-log-only-print-member-fields) – Bergi Jan 15 '14 at 05:00

1 Answers1

3

Properties of prototypes are not printed out by default. The ^^ operator sets the operand as the prototype of a new object. The properties are still are still accessible, but won't be printed by console.log and won't be serialized into JSON.

If you simply want to copy over properties, use {} <<< obj.

gkz
  • 416
  • 3
  • 5