Created
December 13, 2013 07:55
-
-
Save CatTail/7941112 to your computer and use it in GitHub Desktop.
Use a function to mock new operator
This file contains hidden or 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
| /** | |
| * Mock new operator. | |
| * Worked just as new operator. | |
| * @param {Function} Type | |
| * @param {...*} opt_args | |
| * @return {*} | |
| */ | |
| var newFunc = function(Type, opt_args) { | |
| var Ctor, proto; | |
| Ctor = function(){}; | |
| Ctor.prototype = Type.prototype; | |
| proto = new Ctor(); | |
| return Type.apply(proto, Array.prototype.slice.call(arguments, 1)) || proto; | |
| }; |
This file contains hidden or 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
| describe('newFunc', function() { | |
| it('should act as new operator', function() { | |
| var Ctor = function(value){ this.key = value; }; | |
| var value = 'test-value'; | |
| var obj = qd.newFunc(Ctor, value); | |
| expect(obj instanceof Ctor).toEqual(true); | |
| expect(obj.key).toEqual(value); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment