Javascript - Create instance with array of arguments - javascript
I know the possibility to call a function with an array of arguments with apply(obj,args);
Is there a way to use this feature when creating a new instance of a function?
I mean something like this:
function A(arg1,arg2){
var a = arg1;
var b = arg2;
}
var a = new A.apply([1,2]); //create new instance using an array of arguments
I hope you understand what i mean... ^^^
Thanks for your help!
Solved!
I got the right answer. To make the answer fit to my question:
function A(arg1,arg2) {
var a = arg1;
var b = arg2;
}
var a = new (A.bind.apply(A,[A,1,2]))();
var wrapper = function(f, args) {
return function() {
f.apply(this, args);
};
};
function Constructor() {
this.foo = 4;
}
var o = new (wrapper(Constructor, [1,2]));
alert(o.foo);
We take a function and arguments and create a function that applies the arguments to that function with the this scope.
Then if you call it with the new keyword it passes in a new fresh this and returns it.
The important thing is the brackets
new (wrapper(Constructor, [1,2]))
Calls the new keyword on the function returned from the wrapper, where as
new wrapper(Constructor, [1,2])
Calls the new keyword on the wrapper function.
The reason it needs to be wrapped is so that this that you apply it to is set with the new keyword. A new this object needs to be created and passed into a function which means that you must call .apply(this, array) inside a function.
Live example
Alternatively you could use ES5 .bind method
var wrapper = function(f, args) {
var params = [f].concat(args);
return f.bind.apply(f, params);
};
See example
with ECMAscript 5 you can:
function createInstanceWithArguments (fConstructor, aArgs) {
var foo = Object.create(fConstructor.prototype);
fConstructor.apply(foo, aArgs);
return foo;
}
#Raynos answer works well, except that the non-ES5 version is missing the constructor's prototype after instantiation.
Here's my updated cApply method:
var cApply = function(c) {
var ctor = function(args) {
c.apply(this, args);
};
ctor.prototype = c.prototype;
return ctor;
};
Which can be used like this:
var WrappedConstructor = cApply(Constructor);
var obj = new WrappedConstructor([1,2,3]);
// or inline like this.
var obj2 = new (cApply(Constructor))([1,2,3]);
JSFiddle for reference.
You can curry the functions:
function A(arg1, arg2) {
// ...
}
function A12() {
A(1, 2);
}
You could even build a curry factory:
function A_curry() {
var args = arguments;
return function () {
A.apply(null, args);
};
}
You can use Object.create() to build the new instance, and call the constructor on the instance after that:
var args = [1,2,3];
var instance = Object.create(MyClass.prototype);
MyClass.apply(instance, args);
or in a single line:
var instance = MyClass.apply(Object.create(MyClass.prototype), args);
but don't forget return this; in MyClass by this single line solution.
If you don't have Object.create(), then it is very simple to write one:
Object.create = function (source){
var Surrogate = function (){};
Surrogate.prototype = source;
return new Surrogate();
};
You can optionally write an Function.newInstance() or a Function.prototype.newInstance() function:
Function.newInstance = function (fn, args){
var instance = Object.create(fn.prototype);
fn.apply(instance, args);
return instance;
};
so var instance = Function.newInstance(MyClass, args);.
note: It is not recommended to override native classes.
What if you have your object class name stored in a variable called className ?
var className = 'A'
According to this answer here it all becomes very clean and simple using ECMAScipt5's Function.prototype.bind method.
With an array of arguments:
new ( Function.prototype.bind.apply( className, arguments ) );
Or with an argument list:
new ( Function.prototype.bind.call( className, argument1, argument2, ... ) );
A single line, no need for a wrapper.
I just had the same issue and also wanted to preserve prototype properties of the target object. After looking at the answers, just came up with a rather simple solution. Thought I might as well share it for any future seeker :)
// Define a pseudo object that takes your arguments as an array
var PseudoObject = function (args) {
TargetObject.apply(this, args);
};
// if you want to inherit prototype as well
PseudoObject.prototype = new TargetObject();
// Now just use the PseudoObject to instantiate a object of the the OtherObject
var newObj = new PseudoObject([1, 2, 3]);
It can now be done using the spread operator:
let a = new A(...[1,2]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#examples
Related
Any way to reseat a Function object's "this" reference BEFORE constructor invocation?
Trying to implement a Javascript sandboxing scheme and now I've run into a bit of a wrinkle. I need to pass a "context" parameter to the code which will essentially serve as a handle to the "global" object, but so far no luck. To illustrate the problem with a simple example, consider this bit of code: var foo = new Function(" this.baz = this.mux; return this "); foo.mux = "mux"; foo.call(); console.log(foo.baz); console.log(foo.toString()); Output: $> undefined $> function anonymous() { this.mux; return this; } It obviously doesn't work because the Function object doesn't seem to get it's own this like ordinary functions created with new. So...is there any way to "reseat" a Function's this to point to itself beforehand (or just any other way around the issue)? EDIT Okay, so from what I understand from the comments section I'm going to need a constructed object. var foo = new Function(" return new function(){ this.baz /* = ?? */; return this; } "); Is there a way to somehow access the enclosing anonymous function's properties? Like "this.mux = foo.mux" (except of course "foo" isn't visible from that scope)?
I think your getting confused on what new Function( does,. It does not create an instance of an object, it just create a function. So like any object instances you will also need to use new on these. So you need 2 steps.. create the function that you will be creating an object from.. with this function create an instance using new.. Below is a simple example.. var fcreate = new Function('global', "this.global = global"); var f = new fcreate("hello"); console.log(f.global); If your not bothered about instances, we can forget about this altogether, and just create a captured scope as a parameter.. eg.. var f = new Function("global", "console.log(global)"); f("This is a global to function"); f("This is another one");
You can pass foo as a parameter of call: var foo = new Function(" this.baz = this.mux; return this "); foo.mux = "mux"; foo.call(foo); // <-- this Edit: Although the code above works, I wouldn't recommend it. You will be better off creating the function/class foo: var Foo = function(mux){ this.baz = mux; } var foo = new Foo("mux"); console.log(foo.baz);
The best I could come up with that actually works. var foo = new Function(" this.baz = this.mux; return this "); var context = { mux: "mux" }; foo = foo.bind(context); foo(); // context.baz == "mux"
Alright so this is in fact doable, and it's basically an extension of Keith's answer: function verify(condition) { console.log(condition === true ? "pass" : "fail"); } function test() { if(!(this instanceof test)) return new test(); var foo = new Function("mux", "return new function(){ this.baz = mux; return this; } "); var bar = new foo(null); verify(bar.baz === null); var zim = new foo(this); verify(zim.baz === this); var qud = new foo(global); verify(qud.baz === global); }; test(); Output: pass pass pass A sincere thanks to everyone for helping me figure this one out - cheers! * EDIT * As per Keith's comments, the correct implementation would simply be: function verify(condition) { console.log(condition === true ? "pass" : "fail"); } function test() { if(!(this instanceof test)) return new test(); var foo = new Function("mux", "this.baz = mux; return this; "); var bar = new foo(null); verify(bar.baz === null); var zim = new foo(this); verify(zim.baz === this); var qud = new foo(global); verify(qud.baz === global); }; test();
Pass array into google.maps.LatLng via apply() [duplicate]
In JavaScript, I want to create an object instance (via the new operator), but pass an arbitrary number of arguments to the constructor. Is this possible? What I want to do is something like this (but the code below does not work): function Something(){ // init stuff } function createSomething(){ return new Something.apply(null, arguments); } var s = createSomething(a,b,c); // 's' is an instance of Something The Answer From the responses here, it became clear that there's no built-in way to call .apply() with the new operator. However, people suggested a number of really interesting solutions to the problem. My preferred solution was this one from Matthew Crumley (I've modified it to pass the arguments property): var createSomething = (function() { function F(args) { return Something.apply(this, args); } F.prototype = Something.prototype; return function() { return new F(arguments); } })();
With ECMAScript5's Function.prototype.bind things get pretty clean: function newCall(Cls) { return new (Function.prototype.bind.apply(Cls, arguments)); // or even // return new (Cls.bind.apply(Cls, arguments)); // if you know that Cls.bind has not been overwritten } It can be used as follows: var s = newCall(Something, a, b, c); or even directly: var s = new (Function.prototype.bind.call(Something, null, a, b, c)); var s = new (Function.prototype.bind.apply(Something, [null, a, b, c])); This and the eval-based solution are the only ones that always work, even with special constructors like Date: var date = newCall(Date, 2012, 1); console.log(date instanceof Date); // true edit A bit of explanation: We need to run new on a function that takes a limited number of arguments. The bind method allows us to do it like so: var f = Cls.bind(anything, arg1, arg2, ...); result = new f(); The anything parameter doesn't matter much, since the new keyword resets f's context. However, it is required for syntactical reasons. Now, for the bind call: We need to pass a variable number of arguments, so this does the trick: var f = Cls.bind.apply(Cls, [anything, arg1, arg2, ...]); result = new f(); Let's wrap that in a function. Cls is passed as argument 0, so it's gonna be our anything. function newCall(Cls /*, arg1, arg2, ... */) { var f = Cls.bind.apply(Cls, arguments); return new f(); } Actually, the temporary f variable is not needed at all: function newCall(Cls /*, arg1, arg2, ... */) { return new (Cls.bind.apply(Cls, arguments))(); } Finally, we should make sure that bind is really what we need. (Cls.bind may have been overwritten). So replace it by Function.prototype.bind, and we get the final result as above.
Here's a generalized solution that can call any constructor (except native constructors that behave differently when called as functions, like String, Number, Date, etc.) with an array of arguments: function construct(constructor, args) { function F() { return constructor.apply(this, args); } F.prototype = constructor.prototype; return new F(); } An object created by calling construct(Class, [1, 2, 3]) would be identical to an object created with new Class(1, 2, 3). You could also make a more specific version so you don't have to pass the constructor every time. This is also slightly more efficient, since it doesn't need to create a new instance of the inner function every time you call it. var createSomething = (function() { function F(args) { return Something.apply(this, args); } F.prototype = Something.prototype; return function(args) { return new F(args); } })(); The reason for creating and calling the outer anonymous function like that is to keep function F from polluting the global namespace. It's sometimes called the module pattern. [UPDATE] For those who want to use this in TypeScript, since TS gives an error if F returns anything: function construct(constructor, args) { function F() : void { constructor.apply(this, args); } F.prototype = constructor.prototype; return new F(); }
If your environment supports ECMA Script 2015's spread operator (...), you can simply use it like this function Something() { // init stuff } function createSomething() { return new Something(...arguments); } Note: Now that the ECMA Script 2015's specifications are published and most JavaScript engines are actively implementing it, this would be the preferred way of doing this. You can check the Spread operator's support in few of the major environments, here.
Suppose you've got an Items constructor which slurps up all the arguments you throw at it: function Items () { this.elems = [].slice.call(arguments); } Items.prototype.sum = function () { return this.elems.reduce(function (sum, x) { return sum + x }, 0); }; You can create an instance with Object.create() and then .apply() with that instance: var items = Object.create(Items.prototype); Items.apply(items, [ 1, 2, 3, 4 ]); console.log(items.sum()); Which when run prints 10 since 1 + 2 + 3 + 4 == 10: $ node t.js 10
In ES6, Reflect.construct() is quite convenient: Reflect.construct(F, args)
#Matthew I think it's better to fix the constructor property also. // Invoke new operator with arbitrary arguments // Holy Grail pattern function invoke(constructor, args) { var f; function F() { // constructor returns **this** return constructor.apply(this, args); } F.prototype = constructor.prototype; f = new F(); f.constructor = constructor; return f; }
You could move the init stuff out into a separate method of Something's prototype: function Something() { // Do nothing } Something.prototype.init = function() { // Do init stuff }; function createSomething() { var s = new Something(); s.init.apply(s, arguments); return s; } var s = createSomething(a,b,c); // 's' is an instance of Something
An improved version of #Matthew's answer. This form has the slight performance benefits obtained by storing the temp class in a closure, as well as the flexibility of having one function able to be used to create any class var applyCtor = function(){ var tempCtor = function() {}; return function(ctor, args){ tempCtor.prototype = ctor.prototype; var instance = new tempCtor(); ctor.prototype.constructor.apply(instance,args); return instance; } }(); This would be used by calling applyCtor(class, [arg1, arg2, argn]);
This answer is a little late, but figured anyone who sees this might be able to use it. There is a way to return a new object using apply. Though it requires one little change to your object declaration. function testNew() { if (!( this instanceof arguments.callee )) return arguments.callee.apply( new arguments.callee(), arguments ); this.arg = Array.prototype.slice.call( arguments ); return this; } testNew.prototype.addThem = function() { var newVal = 0, i = 0; for ( ; i < this.arg.length; i++ ) { newVal += this.arg[i]; } return newVal; } testNew( 4, 8 ) === { arg : [ 4, 8 ] }; testNew( 1, 2, 3, 4, 5 ).addThem() === 15; For the first if statement to work in testNew you have to return this; at the bottom of the function. So as an example with your code: function Something() { // init stuff return this; } function createSomething() { return Something.apply( new Something(), arguments ); } var s = createSomething( a, b, c ); Update: I've changed my first example to sum any number of arguments, instead of just two.
I just came across this problem, and I solved it like this: function instantiate(ctor) { switch (arguments.length) { case 1: return new ctor(); case 2: return new ctor(arguments[1]); case 3: return new ctor(arguments[1], arguments[2]); case 4: return new ctor(arguments[1], arguments[2], arguments[3]); //... default: throw new Error('instantiate: too many parameters'); } } function Thing(a, b, c) { console.log(a); console.log(b); console.log(c); } var thing = instantiate(Thing, 'abc', 123, {x:5}); Yeah, it's a bit ugly, but it solves the problem, and it's dead simple.
if you're interested in an eval-based solution function createSomething() { var q = []; for(var i = 0; i < arguments.length; i++) q.push("arguments[" + i + "]"); return eval("new Something(" + q.join(",") + ")"); }
This works! var cls = Array; //eval('Array'); dynamically var data = [2]; new cls(...data);
See also how CoffeeScript does it. s = new Something([a,b,c]...) becomes: var s; s = (function(func, args, ctor) { ctor.prototype = func.prototype; var child = new ctor, result = func.apply(child, args); return Object(result) === result ? result : child; })(Something, [a, b, c], function(){});
This constructor approach works both with and without the new keyword: function Something(foo, bar){ if (!(this instanceof Something)){ var obj = Object.create(Something.prototype); return Something.apply(obj, arguments); } this.foo = foo; this.bar = bar; return this; } It assumes support for Object.create but you could always polyfill that if you're supporting older browsers. See the support table on MDN here. Here's a JSBin to see it in action with console output.
Solution without ES6 or polyfills: var obj = _new(Demo).apply(["X", "Y", "Z"]); function _new(constr) { function createNamedFunction(name) { return (new Function("return function " + name + "() { };"))(); } var func = createNamedFunction(constr.name); func.prototype = constr.prototype; var self = new func(); return { apply: function(args) { constr.apply(self, args); return self; } }; } function Demo() { for(var index in arguments) { this['arg' + (parseInt(index) + 1)] = arguments[index]; } } Demo.prototype.tagged = true; console.log(obj); console.log(obj.tagged); output Demo {arg1: "X", arg2: "Y", arg3: "Z"} ... or "shorter" way: var func = new Function("return function " + Demo.name + "() { };")(); func.prototype = Demo.prototype; var obj = new func(); Demo.apply(obj, ["X", "Y", "Z"]); edit: I think this might be a good solution: this.forConstructor = function(constr) { return { apply: function(args) { let name = constr.name.replace('-', '_'); let func = (new Function('args', name + '_', " return function " + name + "() { " + name + "_.apply(this, args); }"))(args, constr); func.constructor = constr; func.prototype = constr.prototype; return new func(args); }}; }
You can't call a constructor with a variable number of arguments like you want with the new operator. What you can do is change the constructor slightly. Instead of: function Something() { // deal with the "arguments" array } var obj = new Something.apply(null, [0, 0]); // doesn't work! Do this instead: function Something(args) { // shorter, but will substitute a default if args.x is 0, false, "" etc. this.x = args.x || SOME_DEFAULT_VALUE; // longer, but will only put in a default if args.x is not supplied this.x = (args.x !== undefined) ? args.x : SOME_DEFAULT_VALUE; } var obj = new Something({x: 0, y: 0}); Or if you must use an array: function Something(args) { var x = args[0]; var y = args[1]; } var obj = new Something([0, 0]);
Matthew Crumley's solutions in CoffeeScript: construct = (constructor, args) -> F = -> constructor.apply this, args F.prototype = constructor.prototype new F or createSomething = (-> F = (args) -> Something.apply this, args F.prototype = Something.prototype return -> new Something arguments )()
function createSomething() { var args = Array.prototype.concat.apply([null], arguments); return new (Function.prototype.bind.apply(Something, args)); } If your target browser doesn't support ECMAScript 5 Function.prototype.bind, the code won't work. It is not very likely though, see compatibilty table.
modified #Matthew answer. Here I can pass any number of parameters to function as usual (not array). Also 'Something' is not hardcoded into: function createObject( constr ) { var args = arguments; var wrapper = function() { return constr.apply( this, Array.prototype.slice.call(args, 1) ); } wrapper.prototype = constr.prototype; return new wrapper(); } function Something() { // init stuff }; var obj1 = createObject( Something, 1, 2, 3 ); var same = new Something( 1, 2, 3 );
This one-liner should do it: new (Function.prototype.bind.apply(Something, [null].concat(arguments)));
While the other approaches are workable, they're unduly complex. In Clojure you generally create a function that instantiates types/records and use that function as the mechanism for instantiation. Translating this to JavaScript: function Person(surname, name){ this.surname = surname; this.name = name; } function person(surname, name){ return new Person(surname, name); } By taking this approach you avoid the use of new except as described above. And this function, of course, has no issues working with apply or any number of other functional programming features. var doe = _.partial(person, "Doe"); var john = doe("John"); var jane = doe("Jane"); By using this approach, all of your type constructors (e.g. Person) are vanilla, do-nothing constructors. You just pass in arguments and assign them to properties of the same name. The hairy details go in the constructor function (e.g. person). It is of little bother having to create these extra constructor functions since they are a good practice anyhow. They can be convenient since they allow you to potentially have several constructor functions with different nuances.
It's also intresting to see how the issue of reusing the temporary F() constructor, was addressed by using arguments.callee, aka the creator/factory function itself: http://www.dhtmlkitchen.com/?category=/JavaScript/&date=2008/05/11/&entry=Decorator-Factory-Aspect
Any function (even a constructor) can take a variable number of arguments. Each function has an "arguments" variable which can be cast to an array with [].slice.call(arguments). function Something(){ this.options = [].slice.call(arguments); this.toString = function (){ return this.options.toString(); }; } var s = new Something(1, 2, 3, 4); console.log( 's.options === "1,2,3,4":', (s.options == '1,2,3,4') ); var z = new Something(9, 10, 11); console.log( 'z.options === "9,10,11":', (z.options == '9,10,11') ); The above tests produce the following output: s.options === "1,2,3,4": true z.options === "9,10,11": true
Here is my version of createSomething: function createSomething() { var obj = {}; obj = Something.apply(obj, arguments) || obj; obj.__proto__ = Something.prototype; //Object.setPrototypeOf(obj, Something.prototype); return o; } Based on that, I tried to simulate the new keyword of JavaScript: //JavaScript 'new' keyword simulation function new2() { var obj = {}, args = Array.prototype.slice.call(arguments), fn = args.shift(); obj = fn.apply(obj, args) || obj; Object.setPrototypeOf(obj, fn.prototype); //or: obj.__proto__ = fn.prototype; return obj; } I tested it and it seems that it works perfectly fine for all scenarios. It also works on native constructors like Date. Here are some tests: //test new2(Something); new2(Something, 1, 2); new2(Date); //"Tue May 13 2014 01:01:09 GMT-0700" == new Date() new2(Array); //[] == new Array() new2(Array, 3); //[undefined × 3] == new Array(3) new2(Object); //Object {} == new Object() new2(Object, 2); //Number {} == new Object(2) new2(Object, "s"); //String {0: "s", length: 1} == new Object("s") new2(Object, true); //Boolean {} == new Object(true)
Yes we can, javascript is more of prototype inheritance in nature. function Actor(name, age){ this.name = name; this.age = age; } Actor.prototype.name = "unknown"; Actor.prototype.age = "unknown"; Actor.prototype.getName = function() { return this.name; }; Actor.prototype.getAge = function() { return this.age; }; when we create an object with "new" then our created object INHERITS getAge(), But if we used apply(...) or call(...) to call Actor, then we are passing an object for "this" but the object we pass WON'T inherit from Actor.prototype unless, we directly pass apply or call Actor.prototype but then.... "this" would point to "Actor.prototype" and this.name would write to: Actor.prototype.name. Thus affecting all other objects created with Actor...since we overwrite the prototype rather than the instance var rajini = new Actor('Rajinikanth', 31); console.log(rajini); console.log(rajini.getName()); console.log(rajini.getAge()); var kamal = new Actor('kamal', 18); console.log(kamal); console.log(kamal.getName()); console.log(kamal.getAge()); Let's try with apply var vijay = Actor.apply(null, ["pandaram", 33]); if (vijay === undefined) { console.log("Actor(....) didn't return anything since we didn't call it with new"); } var ajith = {}; Actor.apply(ajith, ['ajith', 25]); console.log(ajith); //Object {name: "ajith", age: 25} try { ajith.getName(); } catch (E) { console.log("Error since we didn't inherit ajith.prototype"); } console.log(Actor.prototype.age); //Unknown console.log(Actor.prototype.name); //Unknown By passing Actor.prototype to Actor.call() as the first argument, when the Actor() function is ran, it executes this.name=name, Since "this" will point to Actor.prototype, this.name=name; means Actor.prototype.name=name; var simbhu = Actor.apply(Actor.prototype, ['simbhu', 28]); if (simbhu === undefined) { console.log("Still undefined since the function didn't return anything."); } console.log(Actor.prototype.age); //simbhu console.log(Actor.prototype.name); //28 var copy = Actor.prototype; var dhanush = Actor.apply(copy, ["dhanush", 11]); console.log(dhanush); console.log("But now we've corrupted Parent.prototype in order to inherit"); console.log(Actor.prototype.age); //11 console.log(Actor.prototype.name); //dhanush Coming back to orginal question how to use new operator with apply, here is my take.... Function.prototype.new = function(){ var constructor = this; function fn() {return constructor.apply(this, args)} var args = Array.prototype.slice.call(arguments); fn.prototype = this.prototype; return new fn }; var thalaivar = Actor.new.apply(Parent, ["Thalaivar", 30]); console.log(thalaivar);
since ES6 this is possible through the Spread operator, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator#Apply_for_new This answer was already, sort of given in comment https://stackoverflow.com/a/42027742/7049810, but seems to have been missed by most
Actually the simplest method is: function Something (a, b) { this.a = a; this.b = b; } function createSomething(){ return Something; } s = new (createSomething())(1, 2); // s == Something {a: 1, b: 2}
A revised solution from #jordancpaul's answer. var applyCtor = function(ctor, args) { var instance = new ctor(); ctor.prototype.constructor.apply(instance, args); return instance; };
Make an anonymous prototype and apply the Something prototype to it using the arguments and then create a new instance of that anonymous prototype. The one disadavantage of this is it will not pass the s instanceof Something check, though it is identical, it is basically an instance of a clone. function Something(){ // init stuff } function createSomething(){ return new (function(){Something.apply(this, arguments)}); } var s = createSomething(a,b,c); // 's' is an instance of Something
function FooFactory() { var prototype, F = function(){}; function Foo() { var args = Array.prototype.slice.call(arguments), i; for (i = 0, this.args = {}; i < args.length; i +=1) { this.args[i] = args[i]; } this.bar = 'baz'; this.print(); return this; } prototype = Foo.prototype; prototype.print = function () { console.log(this.bar); }; F.prototype = prototype; return Foo.apply(new F(), Array.prototype.slice.call(arguments)); } var foo = FooFactory('a', 'b', 'c', 'd', {}, function (){}); console.log('foo:',foo); foo.print();
JavaScript - Meaning of: var DndUpload = function (inputElem){};
Can I kindly ask for explanation: What does the code below represent? Does it create a DndUpload Ojbect? Or, does it create a DndUpload() function? What I miss is the statement new normally present during JavaScript objects creation. Can I kindly ask for some explanation, as I am confused. var DndUpload = function (inputElem) { this.input = inputElem; this.dropZone = null; this.isDragging = false; this.init(); }; As far as I know this is the way to create object in Javascript: var myObject = new function() { }; If you have any link with explanation, that would help. Thank you.
It's a worse way of writing this: function DndUpload(inputElem) { this.input = inputElem; this.dropZone = null; this.isDragging = false; this.init(); } which is a function declaration. It does not create an instance of DndUpload. Technically, it does create an object – its name is DndUpload and it is an instance of Function. To create an instance of this "class:" var instance = new DndUpload(document.getElementById('someInputId'));
var myObject = new function() { }; Defines an anonymous constructor function and then instantiates a new object using the anonymous constructor function. It could have been replaced with var myObject = {}. var DndUpload = function (inputElem) { this.input = inputElem; this.dropZone = null; this.isDragging = false; this.init(); }; Defines a constructor function (technically an anonymous constructor function assigned to a variable). You can then create objects of this "class" by invoking the constructor function with new: var dndUploadObject = new DnDUpload(), anotherUploadObject = new DnDUpload(); //2 unique DnDUpload objects
the code you have essentially creates a constructor for a "class" it's more or less a blueprint for an object. It then puts that constructor into a variable called DndUpload So you can now make an object with var myObject = new DndUpload(input elem)
Call string constructor function
I have a constructor function, for example webapp.core.cf1 or Backbone.Router. But I get this constructor function not as a reference, but as a string! I cannot change this requirement, the constr. must be in a string. How can I make a new object from this, for instance classfun("Backbone.Router")? function classfun (cfstr) { ... cf = new window[cfstr](); If I try it this way, I get the error: ... is not a constructor. Why this does not work? Is there an alternative way without using eval()? Thanks a lot in advance EDIT Thank you all for your answers! Thank you, Tronix117, this was the problem!! Thank you, Benjamin Schulte, for the function!
If you try in your console: var A=function(a){this.test=a}; b = new window["A"](3); console.log(b.test); // return 3 So that means, you are trying to access something which is not in the scope of window, it should rather be something like that: window.something["A"], if you don't know what is something then you should use the function of Benjamin, otherwise use this one, because it's way faster.
You can do it this way (if I understand you correctly): function getObjectByName(name) { var nameParts = name.split('.'); var nameLength = nameParts.length; var scope = window; for (var i=0; i<nameLength; ++i) { scope = scope[nameParts[i]]; } return scope; } var ObjectClass = getObjectByName(cfstr) new ObjectClass(); Can still be optimized, but should work this way.
ReflectUtil.newInstance = function(strClass) { var args = Array.prototype.slice.call(arguments, 1); var clsClass = eval(strClass); function F() { return clsClass.apply(this, args); } F.prototype = clsClass.prototype; return new F(); }; SomeClass = function(arg1, arg2) { // ... } ReflectUtil.newInstance('SomeClass', 5, 7); This will allow you to instantiate an object by its name and allow you to pass parameters as necessary.
javascript class inherit from Function class
I like that in javascript, I can create a function, and then add further methods and attributes to that function myInstance = function() {return 5} myInstance.attr = 10 I would like to create a class to generate these objects. I assume I have to inherit from the Function base class. In other words, I would like to: var myInstance = new myFunctionClass() var x = myInstance() // x == 5 But I don't know how to create the myFunctionClass. I have tried the following, but it does not work: var myFunctionClass = function() {Function.call(this, "return 5")} myFunctionClass.prototype = new Function() myInstance = new myFunctionClass() myInstance() // I would hope this would return 5, but instead I get // TypeError: Property 'myInstance' of object #<Object> is not a function I also tried the more complicated (and more proper?) inheritance method found here: How to "properly" create a custom object in JavaScript?, with no more luck. I have also tried using the util.inherits(myFunctionClass, Function) found in node.js. Still no luck I have exhausted Google, and therefore feel that I must be missing something fundamental or obvious. Help would be greatly appreciated.
Your trying to inherit from Function. This is a right pain to do. I suggest you do the following instead Live Example var Proto = Object.create(Function.prototype); Object.extend(Proto, { constructor: function (d) { console.log("construct, argument : ", d); this.d = d; // this is your constructor logic }, call: function () { console.log("call", this.d); // this get's called when you invoke the "function" that is the instance return "from call"; }, method: function () { console.log("method"); // some method return "return from method"; }, // some attr attr: 42 }); You want to create a prototype object that forms the basis of your "class". It has your generic methods/attributes. It also has a constructor that gets invoked on object construction and a call method that gets invoked when you call the function var functionFactory = function (proto) { return function () { var f = function () { return f.call.apply(f, arguments); }; Object.keys(proto).forEach(function (key) { f[key] = proto[key]; }); f.constructor.apply(f, arguments); return f; } } A function factory takes a prototype object and returns a factory for it. The returned function when called will give you a new function object that "inherits" from your prototype object. var protoFactory = functionFactory(proto); var instance = protoFactory(); Here you create your factory and then create your instance. However this isn't proper prototypical OO. we are just shallow copying properties of a prototype into a new object. So changes to the prototype will not reflect back to the original object. If you want real prototypical OO then you need to use a hack. var f = function () { // your logic here }; f.__proto__ = Proto; Notice how we use the non-standard deprecated .__proto__ and we are mutating the value of [[Prototype]] at run-time which is considered evil.
JS does not allow a constructor to return a function, even though functions are objects. So you cant have an instantiation of a prototype that is itself executable. (Am I right in this? please correct if I'm not, it's an interesting question). Though you could do a factory function: var makeCoolFunc = function() { var f = function() { return 5 }; f.a = 123; f.b = 'hell yes!' return f; }; var func = makeCoolFunc(); var x = func();
You can extend Function and pass the wanted function body as String to the super constructor. The context of the function can be accessed with arguments.callee. Example for an observable Attribute class: export default class Attribute extends Function { constructor(defaultValue){ super("value", "return arguments.callee.apply(arguments);"); this.value = defaultValue; this.defaultValue = defaultValue; this.changeListeners = []; } apply([value]){ if(value!==undefined){ if(value!==this.value){ var oldValue = this.value; this.value=value; this.changeListeners.every((changeListener)=>changeListener(oldValue, value)); } } return this.value; } clear(){ this.value=undefined; } reset(){ this.value=this.defaultValue; } addChangeListener(listener){ this.changeListeners.push(listener); } removeChangeListener(listener){ this.changeListeners.remove(listener); } clearChangeListeners(){ this.changeListeners = []; } } Example usage: import Attribute from './attribute.js'; var name= new Attribute(); name('foo'); //set value of name to 'foo' name.addChangeListener((oldValue, newValue)=>{ alert('value changed from ' +oldValue+ ' to ' +newValue); }); alert(name()); //show value of name: 'foo' name('baa'); //set value of name to new value 'baa' and trigger change listener