Created
June 20, 2012 15:52
-
-
Save VoQn/2960607 to your computer and use it in GitHub Desktop.
test Object.create
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var clone = function( object ) { | |
var copied = Object.create( Object.getPrototypeOf( object ) ), | |
properties = Object.getOwnPropertyNames( object ), | |
index = 0, | |
name; | |
for ( ; name = properties[ index ]; index++ ){ | |
Object.defineProperty( copied, | |
name, | |
Object.getOwnPropertyDescriptor( object, name ) ); | |
} | |
return copied; | |
}; | |
function Tuple1( f, s ){ | |
this.fst = f; | |
this.snd = s; | |
}; | |
var Tuple2 = function( f, s ){ | |
this.fst = f; | |
this.snd = s; | |
} | |
// test by Chrome | |
var t1 = new Tuple1( 1, 2 ); | |
var t2 = new Tuple2( 1, 2 ); | |
var t1_ = clone( t1 ); // Tuple1 | |
var t2_ = clone( t2 ); // Object (!?) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment