Answer the question
In order to leave comments, you need to log in
What is the principle of the function?
And so there is a function that takes one and the second optional argument, the first is a constructor class, the second is its arguments, and in fact it returns its object, there is a question, new (Class.bind.apply( Class, arguments )),
how he receives his prototype, please help in detail)
function construct(Class/*,arg*/) {
return new (Class.bind.apply( Class, arguments ))()// ???
}
function Class(name){
this.name = name;
}
Class.prototype.sayHello = function(){
return 'Hello' + ' ' + this.name;
}
var obj = construct(Class, 'Steve');
console.log(obj.name); // "Steve"
console.log(obj.sayHello()); // "Hello Steve"
Answer the question
In order to leave comments, you need to log in
In fact, this whole crutch is no different from:
function construct(Class, name) {
return new Class(name);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question