skipzero
5/9/2016 - 3:45 PM

recreate the 'new' keyword using ES6.

recreate the 'new' keyword using ES6.

function newKeyWord(funct) {

	// we're given an array-like object containing all arguments;
	const args = arguments;

	//create our object and link it to the prototype chain;
	let obj = Object.create(funct.prototype);

	// use from to turn our array-like into an array;
	args = Array.from(args)

	// remove our first arguement as it's our constructor function (funct);
	args.shift();

	// apply our constructor function to our obj and provided args;
	funct.apply(obj, args);

	// return our object after processing;
	return obj;
}

// export our keyword module to use in place of the native 'new' keyword;
module.exports = newKeyWord;