最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Programmatically create a new object - Stack Overflow

programmeradmin2浏览0评论

I am trying to create a function which will take arguments arg1, arg2... then pass them into a constructor for a new object C like so: new C(arg1, arg2...), so to make a new instance of C the user would simply have to call C(arg) instead of new C(arg). Here is my first attempt:

var C = function(a){ this.a = a; }

var Cn = function(){
    new C.apply(this, arguments);
}

Cn(0)     // Should make a new C with a property a equal to 0
new C(0)  // ie the same as this

Edit: Note, I need it to take an arbitrary number of arguments and not use eval. I'm making a library implementing Algebraic Data Types in js.


Edit: The solution was to take Jeremy's Idea and adapt it to take an unbounded number of arguments:

var C = function() {

    // A unique object so we can identify when we used the 'newless' constructor
    var newlessConstructorObj = {}

    // Check to see if C has been called with `new`
    if(!(this instanceof C))
        // If not pass arguments as single arg back to C
        return new C(newlessConstructorObj, arguments);


    // Check to see if we got here from the line above, if so the arguments were passed in the second arg
    var args = (arguments[0] === newlessConstructorObj) ? arguments[1] : arguments

    // Do stuff with args
    this.a = args[0];
}

C(0);
new C(0);

I am trying to create a function which will take arguments arg1, arg2... then pass them into a constructor for a new object C like so: new C(arg1, arg2...), so to make a new instance of C the user would simply have to call C(arg) instead of new C(arg). Here is my first attempt:

var C = function(a){ this.a = a; }

var Cn = function(){
    new C.apply(this, arguments);
}

Cn(0)     // Should make a new C with a property a equal to 0
new C(0)  // ie the same as this

Edit: Note, I need it to take an arbitrary number of arguments and not use eval. I'm making a library implementing Algebraic Data Types in js.


Edit: The solution was to take Jeremy's Idea and adapt it to take an unbounded number of arguments:

var C = function() {

    // A unique object so we can identify when we used the 'newless' constructor
    var newlessConstructorObj = {}

    // Check to see if C has been called with `new`
    if(!(this instanceof C))
        // If not pass arguments as single arg back to C
        return new C(newlessConstructorObj, arguments);


    // Check to see if we got here from the line above, if so the arguments were passed in the second arg
    var args = (arguments[0] === newlessConstructorObj) ? arguments[1] : arguments

    // Do stuff with args
    this.a = args[0];
}

C(0);
new C(0);
Share edited May 23, 2017 at 11:44 CommunityBot 11 silver badge asked Apr 3, 2012 at 13:24 Callum RogersCallum Rogers 15.9k17 gold badges69 silver badges92 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 2

If you want to be able to call the function with or without the new keyword, you have to follow this pattern:

C = function(a) {
  if (!(this instanceof C)) {
    return new C(a);
  }

  this.a = a;
}

so to create a new instance of "C":

c = new C(a);

and

c = C(a);

will both return a properly formed instance of C

I would have picked Fabrizio Calderan's solution any day, but because you want this specific functionality - here is what predecessors tell us:


you can apply arguments to prototype.constructor (but there are issues when doing it with native types, like Number):

var Cn = function(){
    return C.prototype.constructor.apply(C, arguments);
}

Link: Instantiating a JavaScript object by calling prototype.constructor.apply


or.. use eval:

function construct(Constructor) 
{ 
 /* 
  * or Array.prototype.slice.call(arguments, 1).map(function() { ... }) 
  * in JavaScript 1.6+, patibles, and with augmented Array.prototype 
  */ 
 var args = []; 
 for (var i = 1, len = arguments.length; i < len; i++) 
 { 
   args[i - 1] = "arguments[" + i + "]"; 
 } 

/* or args.join(", ") if you need it pretty-printed */ 
 return eval("new Constructor(" + args + ")"); 
} 

function Foo() 
{ 
  window.alert(Array.prototype.slice.call(arguments, 0).join(", ")); 
} 

var f = construct(Foo, /bar/g, {baz: 42});

Link: http://groups.google./group/p.lang.javascript/browse_thread/thread/ff1a104bdc33d5c8

something like this?

var C = function(obj){ 
  var a;
  for (a in obj) {
     this[a] = obj[a]; 
  }
}
var Cn = function(obj) { return new C(obj); }

instance = Cn({
  a : 1,
  b : 2
})  

instance.a //1
instance.b //2
instance.c //undefined

Doing it with a fixed number of args is simple:

// assuming one arg
function Cn(arg) {
  return new C(arg);
}

// assuming two args
function Cn(arg0, arg1) {
  return new C(arg0, arg1);
}

and so on. You can even make a general version for any number of parameters by iterating over arguments to create a string, then eval it. Crass, but effective.

But what's the point? Just to save typing 4 characters?

If you don't care about having a correct instanceof check you can try this:

var C = function(a){ this.a = a; }

var Cn = function(){
    return C.apply({}, arguments); // notice an empty object here
}

Cn(0)     // Should make a new C with a property a equal to 0
new C(0)  // ie the same as this

Updated for ES6, you can use the spread operator:

var C = function(a){ this.a = a; }

var Cn = function(...args){
    return new C(...args);
}

assert.deepStrictEqual(Cn(10), new C(10));
发布评论

评论列表(0)

  1. 暂无评论