Javascript - The new method -
if break down me, can understand it, i'd greatful. know it's used create new object apply method.
function.prototype.new = function () { var args = arguments; var constructor = this; function fake() { constructor.apply(this, args) }; fake.prototype = constructor.prototype; return new fake; };
basically, function new
used inherit prototype methods on newly created object 1 on being called.
// new function defined on function prototype so, // can called directly function function.prototype.new = function () { // caching arguments array-like object var args = arguments; // inside new() refers function on `new` called var constructor = this; // fake constructor function/class function fake() { // calling function passing context , arguments constructor.apply(this, args) }; // adding prototype members of function on new called // on new fake class fake.prototype = constructor.prototype; // retun new instance of fake class // returning prototypes here return new fake; };
Comments
Post a Comment