31bec3d5f157943932837a7d68d84f3cb84cb7ea
[platform/framework/web/crosswalk-tizen.git] /
1 var baseCreate = require('./baseCreate'),
2     isObject = require('../lang/isObject');
3
4 /**
5  * Creates a function that produces an instance of `Ctor` regardless of
6  * whether it was invoked as part of a `new` expression or by `call` or `apply`.
7  *
8  * @private
9  * @param {Function} Ctor The constructor to wrap.
10  * @returns {Function} Returns the new wrapped function.
11  */
12 function createCtorWrapper(Ctor) {
13   return function() {
14     var thisBinding = baseCreate(Ctor.prototype),
15         result = Ctor.apply(thisBinding, arguments);
16
17     // Mimic the constructor's `return` behavior.
18     // See https://es5.github.io/#x13.2.2 for more details.
19     return isObject(result) ? result : thisBinding;
20   };
21 }
22
23 module.exports = createCtorWrapper;