关于面向对象

一、对象属性设置--defineProperty
1.Object.getOwnPropertyDescriptor()
 var Person={
            name:"zs"
        }
        var des=Object.getOwnPropertyDescriptor(Person,"name")
        console.log(des.value);  //zs
        console.log(des.configurable);  //true
        console.log(des.writable);  //true
        console.log(des.enumerable); //true
2.对象的数据属性
value          属性的属性值
configurable   能否删除属性的值
writable       能否修改属性的值
enumerable     能否遍历(枚举)属性的值


 // var Person={}
        // Object.defineProperty(Person,"name",{value:"zs",writable:false})
        // console.log(Person.name); //zs
        // Person.name="ls"
        // console.log(Person.name);  //zs

var Person={}
Object.defineProperty(Person,"name",{value:"zs",writable:true,enumerable:true})
Object.defineProperty(Person,"age",{value:"23",enumerable:true})
// Object.defineProperty(Person,"age",{value:"23",enumerable:flase})
var v1=Object.keys(Person)
console.log(v1);  //[ "name", "age" ]
3.对象的访问属性
get:在读取属性时调用的函数
set:再写入属性时调用的函数


二、继承
1.约定
function Person(){
    var name="zs"//私有的基本属性
    var friends=["ls","ww"]//私有的引用属性
    function f1() {}   //私有的函数
}

function Person(){
    this.name="zs"//实例的基本属性
    this.friends=["ls","ww"]//实例的引用属性
    this.f1=function f1() {}   //实例的函数
}
function Person(){}
    Person.prototype.name="zs"//原型的基本属性
    Person.prototype.friends=["ls","ww"]//原型的引用属性
    Person.prototype.f1=function f1() {}   //原型的函数
2.原型链继承
创建一个对象,Animal,构造函数+原型的函数
name,引用类型 方法say eat
核心:Cat.prototype=new Animal()
    拿父类实例化充当子类原对象
    原型链查找顺序:先在实例内查找,再找实例对象原型,再找父类构造函数,再找父类圆形
优点:简单易于实现
缺点:原型的引用类型属性是所有实列共享的
     创建子实例时无法向父类构造函数传参
3.原型链原理
function Animal(){}
Animal.prototype.name="animal"
Animal.prototype.say=function(){console.log("hello")}
console.log(Animal)//animal
console.log(typeof Animal.prototype)//object
console.log(Animal.prototype.constructor)//animal

(1)每个函数都有一个显示的"prototype"属性,该属性指向原型对象
(2)原型对象中的"constructor"属性,指向函数本身

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值