Create variable like "new" - Javascript Object

Javascript examples for Object:Constructor

Description

Create variable like "new"

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from   w ww  .j a  v  a  2s  . c  o  m
function Fruit(name) {
   this.name = name;
}
function myNew(con,args){
    return new con(args);
}
var f1 = new Fruit("Mango");
var f2 = myNew(Fruit,"Orange");
console.log(f1.name, f1 instanceof Fruit);
console.log(f2.name, f2 instanceof Fruit);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials