zoukankan      html  css  js  c++  java
  • js中的继承

    1.js继承的入门

    <script >
     function Person(name,age,sex){
     	this.name=name;
     	this.age=age;
     	this.sex=sex;
     }
    Person.prototype.eat=function(){
    	console.log("人可以吃");
    	
    };
     Person.prototype.sleep=function () {
          console.log("人在睡觉");
        };
        Person.prototype.play=function () {
          console.log("生活就是不一样的玩法而已");
       };
    function Student(score){
    	this.score=score;
    	
    }
    
    Student.prototype=new Person("小明",29,"男");
    
    Student.prototype.study=function(){
    	console.log("学习很累");
    };
    var stu=new Student(100);
    console.log(stu.name);
    console.log(stu.age);
    console.log("学生自己的属性和方法");
    stu.study();
    console.log(stu.score);
    </script>
    

    2.js继承的小案例

     //动物的构造
        function Animal(name,weight) {
          this.name=name;
          this.weight=weight;
        }
        //动物的原型的方法
        Animal.prototype.eat=function () {
          console.log("天天吃东西,就是吃");
        };
    
        //狗的构造函数
        function Dog(color) {
          this.color=color;
        }
        Dog.prototype=new Animal("哮天犬","50kg");
        Dog.prototype.bitePerson=function () {
          console.log("哼~汪汪~咬死你");
        };
    
        //哈士奇
        function ErHa(sex) {
          this.sex=sex;
        }
        ErHa.prototype=new Dog("黑白色");
        ErHa.prototype.playHost=function () {
          console.log("哈哈~要坏衣服,要坏桌子,拆家..嘎嘎...好玩,开心不,惊喜不,意外不");
        };
        var erHa=new ErHa("雄性");
        console.log(erHa.name,erHa.weight,erHa.color);
        erHa.eat();
        erHa.bitePerson();
        erHa.playHost();
    

      

    3.js组合继承

     //原型实现继承
        //借用构造函数实现继承
        //组合继承:原型继承+借用构造函数继承
    
        function Person(name,age,sex) {
          this.name=name;
          this.age=age;
          this.sex=sex;
        }
        Person.prototype.sayHi=function () {
          console.log("阿涅哈斯诶呦");
        };
        function Student(name,age,sex,score) {
          //借用构造函数:属性值重复的问题
          Person.call(this,name,age,sex);
          this.score=score;
        }
        //改变原型指向----继承
        Student.prototype=new Person();//不传值
        Student.prototype.eat=function () {
          console.log("吃东西");
        };
        var stu=new Student("小黑",20,"男","100分");
        console.log(stu.name,stu.age,stu.sex,stu.score);
        stu.sayHi();
        stu.eat();
        var stu2=new Student("小黑黑",200,"男人","1010分");
        console.log(stu2.name,stu2.age,stu2.sex,stu2.score);
        stu2.sayHi();
        stu2.eat();
    
        //属性和方法都被继承了
    

      

    4.js拷贝继承

    //拷贝继承;把一个对象中的属性或者方法直接复制到另一个对象中
    	//拷贝继承1
    	 var obj1={
    	 	name:"小糊涂",
    	 	age:20,
    	 	sleep:function(){
    	 		console.log("睡觉了");
    	 	}
    	 };
    	//改变了地址的指向
    	var obj2=obj1;
    	console.log(obj2.name,obj2.age);
    	
    	//拷贝继承2
    	var obj3={};
    	
    	for(var key in obj1){
    		obj3[key]=obj1[key];
    		
    	}
    	console.log(obj3.name,obj3.age);
    	
    	function Person() {
        }
        Person.prototype.age=10;
        Person.prototype.sex="男";
        Person.prototype.height=100;
        Person.prototype.play=function () {
          console.log("玩的好开心");
        };
        var obj5={};
        //Person的构造中有原型prototype,prototype就是一个对象,那么里面,age,sex,height,play都是该对象中的属性或者方法
    
        for(var key in Person.prototype){
          obj5[key]=Person.prototype[key];
        }
        console.dir(obj5);
        obj5.play();
    

      

  • 相关阅读:
    欧拉工程第53题:Combinatoric selections
    540A: Combination Lock
    540C: Ice Cave
    540B :School Marks
    欧拉工程第52题:Permuted multiples
    欧拉工程第51题:Prime digit replacements
    C. Tourist's Notes
    B. Quasi Binary
    [LeetCode] Longest Substring Without Repeating Characters
    [hihoCoder] 后序遍历
  • 原文地址:https://www.cnblogs.com/liushisaonian/p/9459912.html
Copyright © 2011-2022 走看看