<script>
function Star(uname, age) {
this.uname = uname;
this.age = age;
}
// Star.prototype.sing = function () {
// console.log('我会唱歌');
// }
Star.prototype = {
//constructor指向的是构造函数本身,如果使用对象赋值的操作会使得constructor被覆盖,所以必须重新指回构造函数
constructor: Star,
sing: function () {
console.log('我会唱歌');
},
dance: function () {
console.log('我会跳舞');
}
}
//原型对象存放公共的函数和方法
var ldh = new Star('刘德华', 18);
console.log(ldh);
//对象上有系统添加的一个__proto__对象原型,指向我们构造函数的原型对象
ldh.sing();
console.log(ldh.__proto__ === Star.prototype);//true
console.log(ldh.__proto__.sing());
console.log(Star.prototype.dance());
console.log(ldh.__proto__.constructor);//构造函数
console.log(Star.prototype.constructor);//构造函数
console.log(Star.prototype);
</script>
705_构造函数里的constructor
- 本文链接: http://lzkpersonal.com.cn/2023/07/05/705-构造函数里的constructor/
- 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!