prototype
function Star(uname,age) {
this.uname=uname
this.age=age
}
Star.prototype.sing = function() {
console.log('我会唱歌')
}
let wff = new Star('王霏霏',18)
let pxz = new Star('裴秀智',18)
wff.sing()
pxz.sing()
__proto__
(对象都会有一个属性__proto__) console.log(wff.__proto__ === Star.prototype)// 输出结果为true
__proto__
)和构造函数原型对象(prototype
)里面都有一个constructor属性,constructor我们称为构造函数,因为它指回构造函数本身; function Star(uname,age) {
this.uname=uname
this.age=age
}
/*
Star.prototype.sing = function() {
console.log('我会唱歌')
}
Star.prototype.movie = function() {
console.log('我会拍电影')
}
*/
Star.prototype = {
constructor:Star,
sing(){
console.log('我会唱歌')
},
movie(){
console.log('我会拍电影')
}
}
let wff = new Star('王霏霏',18)
let pxz = new Star('裴秀智',18)
console.log(Star.prototype.constructor)
console.log(wff.__proto__.constructor)
注意:如果我们修改了原来的原型对象,并且采取直接给prototype赋值为一个对象的方法(这时候会把原来的constructor属性覆盖掉),则我们必须手动利用constructor这个属性指回原来的构造函数(这样我们才能够知道我们的对象是通过哪一个构造函数创建出来的)。
注意:因此,当我们今后想要利用原型对象扩展内置对象方法的时候,对于数组和字符串内置对象,不能
给原型对象覆盖操作诸如Array.prototype={}
;只能是Array.prototype.xxx=function(){}
的方式。
本文地址:https://blog.csdn.net/wennianzhu/article/details/107528548
如对本文有疑问, 点击进行留言回复!!
offset、client、scroll (width,height、left,top、X,Y)
网友评论