对象
对象
面向对象与原型对象:
面向对象特征:封装、继承、多态。
原型对象含义:原型是Javascript中的继承的基础,JavaScript的继承就是基于原型的继承
与原型有关的几个属性和方法:
1、prototype属性
2、constructor属性
3、__proto__ 属性
4、hasOwnProperty() 方法: 可以判断一个属性是否来自对象本身。
5、isPrototypeOf()方法:检测是一个对象否是是实例对象的原型对象
对象创建新对象:
object.create()
例:
let person = {
eyes: 2,
arms: 2,
run() {
console.log('we can run');
}
}
let hrh = Object.create(person);
console.log(hrh.arms); //2
let hh = Object.create(person, {
name: {
value: 'huronghua',
writable: false,
enumerable: true
},
eyes:{
value:1,
writable: false,
enumerable: true //是否能遍历
}
})
console.log(hh.arms); //2
console.log(hh.eyes); //1
console.log(hh.name); //huronghua
构造函数创建对象:
含义:用于实例化对象的函数
例:
let Peach=function(feel,color){
this.feel=feel,
this.color=color
}
Peach.prototype.sale=function(){
console.log(`Peach is so ${this.feel} `);
}
let peaches=new Peach('sweet','yellow');
console.log(peaches.feel); //sweet
console.log(peaches.color); //yellow
peaches.sale(); //Peach is so sweet
May you live more like yourself than you are bound to.

浙公网安备 33010602011771号