zoukankan      html  css  js  c++  java
  • 对象 原型prototype

    一、对象
     
    1.
    var person={
             name:"kobe",//键值必须用,间隔
             "age":30,
             sayName:function(){
                 console.log("i'm"+this.name);
             }
        }; 


     可配置性:指明该属性是否可以修改、删除 
     可删除configurable:true/false
      若 configurable:false 则delete无效
      可修改 writable:true/false
      枚举性enumerable:true/false  

    var person2={

        };
              //defineProperty是对象提供的方给person2对象添加name属性
        Object.defineProperty(person2,"name",{
           configurable:true,//可删除 默认为true
            writable:true,//可修改 默认为true
            enumerable:true,//可遍历 默认为true
           value:"kobe"
        });


    二、创建对象
    //Json对象
        var person={
            name:"alice",
            age:30,
            sayName:function(){
                console.log(this.name);
            }
        };
            //方式2
            var person={};//创建对象
            person.name="alice";
            person.age=30;
            person.sayName=function(){
                console.log(this.name);
            };

        console.log(person.name);
        console.log(person.age);
        person.sayName();
        //函数
           //方式1 返回Json对象
         function getperson(name,age){
             return{
                 name:name,
                 age:age,
                 sayName:function(){
                     console.log(this.name);
                 }
             };
         }
        var person=getperson("alice",30);
        console.log(person.name);
        console.log(person.age);
        person.sayName();
        //若return为空
        person.name="alice";
        person.age=30;


        //方式2返回Object
        function getPerson(name,age){
            var obj=new Object();
            obj.name=name;
            obj.age=age;
            obj.sayName=function(){
                console.log(this.name);
            };
            return obj;
       }
        var p=getPerson("alice",30);
        console.log(p.name);
        console.log(p.age);
        p.sayName();
        var p2 = rgetPerson("joe",45);
        console.log(p2.name);
        console.log(p2.age);
        p2.sayName();
      
        
        //构造函数 类 类名 首字母大写 会有隐式return,不需要return  用this 创建对象用new
        function Person(name,age){
            this.name=name;//用this
            this.age=age;
            this.sayName=function(){
               console.log(this.name);
            }
        }
        var person=new Person("alice",30);//创建对象 用new
        console.log(person.name);
        console.log(person.age);
        person.sayName();

    三、原型prototype(函数的属性)
          (每个对象都有一个prototype属性,它的属性是对象的类型)

        //定义在原型内   属性共享 只要有任何一个对象改变,其他的也会改变
        function per(){
            this.name="张三";
        }
        per.prototype.arr=[1,2,3,4,5];
        var p1=new per();
        var p2=new per();
        p1.arr.push(6);
        console.log(p1.arr);//[1,2,3,4,5,6]
        console.log(p2.arr);//[1,2,3,4,5,6]
    四、隐式原型__proto__

      _proto_是对象的属性 每个对象都有一个___proto___  属性,指向创建该对象的函数的 prototype

        function person(){}
        var P1=new person();
        console.log(P1.__proto__);
        console.log(person.prototype);
        console.log(P1.__proto__===person.prototype);//true 对象的__proto__属性和函数的 prototype结果一样
    五 原型链
    1、函数里有个Prototype属性,该属性是个对象,对象里有默认的constructor.它指向自身的方法(构造函数)

    2、对象 有个__proto__属性   它的值指向函数的prototype


        //原型链可以继承它原型的属性和方法,不支持多重继承
        //父类
        function Parent(name,age){
             this.name=name;
             this.age=age;
             this.sayName=function(){
                console.log(this.name);
            }
         }
            //指定Parent的原型
            Parent.prototype.sayAge=function(){
                console.log(this.age);
            };

        //子类    Child中可以添加自己的属性比如sex

         function Child(name,age,sex){
             this.constructor(name, age);//constructor是默认得属性 指向obj
             this.sex=sex;

         }
        Child.prototype=new Parent();//通过原型链的方式让Child指向Parent的prototype
        var s=new Child("alice",30);
        s.sayName();
        s.sayAge();


          //## 冒充  ## 只能继承它父类原有的属性和方法,但它prototype的属性和方法不能继承
            function Child2(name, age) {
                this.obj = Parent;//冒充
                this.obj(name, age);//继承
               delete this.obj;//删除继承
        //        Parent.call(this, name, age);
               // Parent.apply(this, [name, age]);//apply里面的是数组,与call方法差不多
            }
            var c = new Child2("zhangsan", 30);
        //  c.sayAge();//说明不能继承的原型的方法
            c.sayName();
          console.log(c.age);



        //定义构造函数worker
        function Worker(pay,work){
            this.pay=pay;
            this.work=work;
        }
          //对象冒充支持多继承 一个类可以继承多个超类
        function Fworker(name,age,pay,work){
            this.inherit=People;//冒充People类
            this.inherit(name,age);
            delete this.inherit;


            this.inherit=Worker;//冒充Worker类
            this.inherit(pay,work);
            delete this.inherit;

        }
        //测试
        var john=new Fworker("John",23,"$1000","teacher");
        john.say();
        console.log(john.work);

    说明:
       1.原型链可以继承它原有的属性·和方法,只能指定一个父类(prototype),但不支持多重继承
       2.继承、冒充只能继承它原有的属性和方法,无法继承它prototype的属性和方法
       3.混合继承与原型链的区别是子类哪里用对象冒充继承他的属性。

  • 相关阅读:
    line
    同步fifo的verilogHDL设计实例
    在DE1-SOC上运行Linux
    DE1-SOC连接设定
    Tcl语言笔记之二
    Tcl语言笔记之一
    关于复位赋初值的问题
    Altera FPGA中的pin进一步说明
    Altera FPGA中的pin简介
    笔记之Cyclone IV第一卷第四章Cyclone IV器件中的嵌入式乘法器
  • 原文地址:https://www.cnblogs.com/alicezq/p/4824111.html
Copyright © 2011-2022 走看看