我已经准备了相当多的博客文章和堆叠溢出的问题,但不能很好地理解这一点。
Why is it necessary to set the prototype constructor?
Trying to understand the difference between prototype and constructor in JavaScript
Constructors in JavaScript objects
JavaScript inheritance and the constructor property
Understanding the difference between Object.create() and new SomeFunction()
问题
function person(fname) {
this.fname = fname;
}
let obj1 = new person('first'); //works well
console.log(obj1);
let obj2 = new person.prototype.constructor('second'); //works well
console.log(obj2);
//now the query
console.log(person.constructor); //1. it prints function Function() { [native code] } what does it mean
let obj3 = new person.constructor('third') // 2. why this doesn't work ?
console.log(obj3); //
现在,我们知道javascript中的每个函数都有一个属性调用prototype,它是一个object,它有一个名为constructor的属性,它指向原型对象是属性的函数。-如果我做错了这件事,纠正我
因此,根据上述声明,person.prototype.constructor === person (两者是相同的)
查询: 1 person.constructor打印function Function() { [native code] }是什么意思,请详细解释。
查询: 2为什么这个new person.constructor('randon')不能工作?
查询: 3,person.constructor到底是什么,它与person.prototype.constructor有什么不同?
发布于 2021-06-22 20:16:49
大致如下:
> let obj3 = new person.constructor("console.log('hi')")
> obj3()
hihttps://stackoverflow.com/questions/68090019
复制相似问题