原型的方法

原型方法

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值