原型方法
Object.setPrototypeOf()
给对象原型设置一个属性指定对应的属性值
Object.getPrototypeOf() (重要)
从自己身上获取的属性(不遵从原型链)
Object.getOwnPropertyNames()
返回string数组 从原型上获取(不遵从原型链)
person.hasOwnProperty() (重要)
对象上是否有这个属性 true
Student.prototype.isPrototypeOf(person1) (重要)
对应的student是否存在person的原型上
console.log(“sex” in person) (重要)
属性是否处在对象上或原型上 布尔类型值
delete person.addr (重要)
delete删除 删除对象的属性
function Person(){ //构造函数
this.username = "张三"
this.age = 250
}
let person = new Person() //对象
person.addr = "湖南"
Object.setPrototypeOf(person,{"sex":'男'}) //给对象原型设置一个属性指定对应的属性值(7)
person.__proto__.color = 'red'
//上述方法跟这个是一样的 person.sex = "男"
function Student(){
}
Person.prototype = Object.create(Student.prototype) //原型继承 Person继承Student
let person1 = new Person() //Object.create(Student.prototype)相当与 new Student()
console.log(person.sex);
console.log(person);
console.log( Object.getPrototypeOf(person)); //不遵从原型链 从自己身上获取的属性 (5)
console.log(Object.getOwnPropertyNames(person) );// 返回string数组 从原型上获取 不遵从原型链 (不包含symbol)(6)
console.log(Object.getOwnPropertyDescriptor(person,'age')); //得到是一个解释器
console.log( Object.defineProperty(person,'aaa',{})); //添加一个属性给原型上生成一个对象
console.log(person.hasOwnProperty("age")); //对象上是否有这个属性 true (1)
console.log(person.hasOwnProperty("sex"));//false
console.log(Student.prototype.isPrototypeOf(person1) );//对应的student是否存在person的原型上 (2)
console.log("sex" in person); //属性是否处在对象上或原型上 布尔类型值 (3)
// delete删除 删除对象的属性 (4)
delete person.addr
console.log(person.addr);