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?
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 byconsole.log
and won't be serialized into JSON.If you simply want to copy over properties, use
{} <<< obj
.