js继承问题

一。对象属性设置--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  能否便利枚举属性的值

 writable :
       var Person = {}
        Object.defineProperty(Person, "name", {
            value: "zs",
            writable: false
        })
        console.log(Person.name)//zs
        Person.name = "ls"//没有修改成功
        console.log(Person.name)//zs

  enumerable:
        var Person = {}
        // Object.defineProperty(Person, "name", {

        //         value: "zs",
        //         writable: false
        //     })
            Object.defineProperty(Person, "age", {

            value: "18",
            enumerable: true
        })
        Object.defineProperty(Person, "age", {

            value: "18",
            enumerable: false
        })

        var v1 = Object.keys(Person)
        console.log(v1) 

 configurable:
         var Person={}
      Object.defineProperty(Person,"name",{value:"zs",configurable:true})
      Object.defineProperty(Person,"age",{value:23,configurable:false})
      delete Person.name
      console.log(Person.name)//undefined
    
      delete Person.age
      console.log(Person.age)//23

三、对象的访问属性
get :在读取属性时调用的函数
set:再写入属性时调用的函数

        var Person = {
            "_name": "zs"
        }
        Object.defineProperty(Person, "name", {


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

        function Person() {
            this.name = "zs" //实例的基本属性
            this.friends = ["ls", "ww"] //实例的引用属性
            this.f1 = function() {} //实例的函数
        }

        function Person() {
            Person.prototype.name = "zs" //原型的基本属性
            Person.prototype.friends = ["ls", "ww"] //原型的引用属性
            Person.prototype.f1 = function() {} //原型的函数
        }
   总结:
   核心:cat.prototype = new animal()拿父类实例化充当子类原型对象
   优点:...简单易于实现   
   原型链查找顺序:先在实例中查找,找实例对象的构造函数,找实例对象的原型,所继承的父类构造函数,找父类原型,
   缺点:原型的应用类型属性是所有实例共享的,
      创建子实例时无法向父类构造函数传参  
          function animal() {
            this.name = "animal"
            this.friends = ['wb', 'b']
            this.say = function() {
                alert("say...")
            }
        }
        animal.prototype = {
            constructor: animal,
            eat: function() {
                alert("我吃")
            }

        }

        function cat() {

        }
        cat.prototype = new animal()
        cat.prototype.name = "cat"
        var c1 = new cat()
        c1.name = "jfcat"
        console.log(c1.name)//jfcat
        c1.friends.push("jfcat01")
        var c2 = new cat()
        console.log(c2.friends)//[]
       三。原型链原理
       (1)每个函数都有一个显示的“prototype”属性,该属性指向原型对象
       (2)原型对象中的“constructor"属性指向函数本身
        function Animal() {}
        // Animal.prototype.name = "animal"
        // Animal.prototype.say = function() {
        //     console.log("hello")
        // }
        // console.log(typeof Animal.prototype) //object
        // console.log(Animal.prototype.constructor) //animal 

   (3)new创建的对象没有prototype属性     
   (4)每一个对象都有一个"_proto_"内部隐藏属性,
   指向他所对应的原型对象(chrome--[[]] firefox--<>)
   (5)原型链利用原型上一个引用类型继承另一个引用类型的方法
   原型链正是基于"_proto_才得以形成

  (6) console.log(Object.prototype.__proto__ == null) //true代表结束
        
        
         function Cat() {}
        Cat.prototype = new Animal()
        console.log(Cat)
        console.log(Cat.prototype)
        console.log(Cat.__proto__)
        var c1 = new Cat()
        var c2 = new Cat()
        console.log(c1.__proto__ == Cat.prototype) //true
        console.log(Cat.prototype.__proto__ == Animal.prototype) //TRUE
        console.log(Object.prototype == Animal.prototype.__proto__) //TRUE
        console.log(Object.prototype.__proto__ == null) //true
4.构造函数继承:       Animal.call(this. arg) 
总结:
    核心:借用父类的构造函数来增强子类的实例,完全没用到原型
    有点:1.解决了子类实例共享父类引用属性的问题
          2.创建子类时可以向父类构造函数传参
    缺点:无法实现函数的复用,每个子类实例都持有一个新的function函数,消耗内存


 function Animal(val) {
            this.name = val
            this.friends = ["a", "b"]
            this.say = function() {
                console.log("say..")
            }

            function Person(arg) {
                Animal.call(this. arg) //继承
            }
        }
        var p1 = new Person("zs")
        console.log(p1.name)
        p2.friends.push("ww")
        var p2 = new Person("ls")
        console.log(p2.name)
        console.log(p2.friends)
        console.log(p1.say == p2.say)
5.组合继承(构造函数+原型链) Animal.call(this, arg)        Person.prototype = new Animal()
总结:
核心:把实例函数都放在原型对象上,以实现函数复用,并保留构造函数的优点,
优点:1,不存在引用属性共享问题,2,可传参,3,函数可复用
缺点:因为父类函数被构造了两次,生成了两份,浪费内存
 function Animal(val) {
            this.name = val
            this.friends = ["a", "b"]
        }
        Animal.prototype.say = function() {
            console.log("say...")
        }

        function Person(arg) {
            Animal.call(this, arg)//继承父类基本属性和引用属性,并保留传参优点
        }
        Person.prototype = new Animal()//继承父类函数,实现了函数复用
        var p1 = new Person("zs")
        console.log(p1.name)//zs
        p1.friends.push("ww")
        var p2 = new Person("LS")
        console.log(p2.name)//ls
        console.log(p2.friends)//[a,b]
        console.log(p1.say == p2.say)//true
 6. 原型式继承
 核心:用生孩子函数得到一个"纯洁"的("没有实例属性)的新对象
 7.寄生式继承
 8.寄生式组合继承--最佳
 优点完美
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值