Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Something(){
- // init stuff
- }
- function createSomething(){
- return new Something.apply(null, arguments);
- }
- var s = createSomething(a,b,c); // 's' is an instance of Something
- var createSomething = (function() {
- function F(args) {
- return Something.apply(this, args);
- }
- F.prototype = Something.prototype;
- return function() {
- return new F(arguments);
- }
- })();
- function construct(constructor, args) {
- function F() {
- return constructor.apply(this, args);
- }
- F.prototype = constructor.prototype;
- return new F();
- }
- var createSomething = (function() {
- function F(args) {
- return Something.apply(this, args);
- }
- F.prototype = Something.prototype;
- return function(args) {
- return new F(args);
- }
- })();
- 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;
- function Something() {
- // init stuff
- return this;
- }
- function createSomething() {
- return Something.apply( new Something(), arguments );
- }
- var s = createSomething( a, b, c );
- 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;
- }
- }();
- var MyClass = function(arg1, arg2){
- };
- //define a class-level create method
- MyClass.create = function(arg1, arg2){
- return new MyClass(arg1, arg2);
- };
- //from with some other method call
- var instance = MyClass.create.apply(this, arguments); //'this' is irrelevant
- 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
- function Items () {
- this.elems = [].slice.call(arguments);
- }
- Items.prototype.sum = function () {
- return this.elems.reduce(function (sum, x) { return sum + x }, 0);
- };
- var items = Object.create(Items.prototype);
- Items.apply(items, [ 1, 2, 3, 4 ]);
- console.log(items.sum());
- $ node t.js
- 10
- function createSomething() {
- var q = [];
- for(var i = 0; i < arguments.length; i++)
- q.push("arguments[" + i + "]");
- return eval("new Something(" + q.join(",") + ")");
- }
- var s;
- s = (function(func, args, ctor) {
- ctor.prototype = func.prototype;
- var child = new ctor, result = func.apply(child, args);
- return typeof result === "object" ? result : child;
- })(Something, [a, b, c], function() {});
- SomeClass = function(arg1, arg2) {
- // ...
- }
- ReflectUtil.newInstance('SomeClass', 5, 7);
- /**
- * @param strClass:
- * class name
- * @param optionals:
- * constructor arguments
- */
- 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();
- };
- construct = (constructor, args) ->
- F = -> constructor.apply this, args
- F.prototype = constructor.prototype
- new F
- createSomething = (->
- F = (args) -> Something.apply this, args
- F.prototype = Something.prototype
- return -> new Something arguments
- )()
- function Something() {
- // deal with the "arguments" array
- }
- var obj = new Something.apply(null, [0, 0]); // doesn't work!
- 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});
- function Something(args) {
- var x = args[0];
- var y = args[1];
- }
- var obj = new Something([0, 0]);
- var Storage = undefined;
- return ((Storage = (new Something(...))) == undefined? (undefined) : (Storage.apply(...)));
Add Comment
Please, Sign In to add comment