为了转生而点技能-JavaScript,day18(Object.create、多层继承

Object.create:
如下图的Dog02利用Object.create来获取Dog01的属性,但是不会显示出来。

        var Dog01 = {            name: 'TOM',            size: 'big',            color: 'yellow'        };        var Dog02 = Object.create(Dog01);        console.log(Dog02);

http://img2.58codes.com/2024/20143762OKBZEQLJiJ.jpg

赋予Dog02新的属性name。

        var Dog02 = Object.create(Dog01);        console.log(Dog02);        Dog02.name = 'Mary';

http://img2.58codes.com/2024/20143762I8TsYmJfpc.jpg



多层继承:

        function Animal(腿) {                    //创一个父层Animal的属性            this.move = '会动';            this.breath = '会呼吸';            this.腿 = 腿 + '条腿';        };                              Animal.prototype.run = function () {    //创一个父层Animal的method            console.log('路上或海上');        };        function dog(name, color) {            //创一个子层dog的属性            Animal.call(this, 4);              //自订子层狗的参数,运用到来自父层animal的属性。            this.name = name;            this.color = color;        };        dog.prototype.eat = function () {      //创一个次层dog的method            console.log('乖乖吃');        };

连接父层Animal与子层dog:
在中间利用Object.create,并利用().prototype.constructor来重建dog的constructor。

        dog.prototype = Object.create(Animal.prototype);        dog.prototype.constructor = dog;
        function Animal(腿) {            this.move = '会动';            this.breath = '会呼吸';            this.腿 = 腿 + '条腿';        };        Animal.prototype.run = function () {            console.log('路上或海上');        };        dog.prototype = Object.create(Animal.prototype);        dog.prototype.constructor = dog;        function dog(name, color) {            Animal.call(this, 4);                          this.name = name;            this.color = color;        };        dog.prototype.eat = function () {            console.log('乖乖吃');        };        var dog01 = new dog('TOM', 'RED');     //利用建构子自订一只实体狗        console.log(dog01);        dog01.eat();        dog01.run();

练习:创造父层animal,及2个子层动物且拥有不同的methods。

        animal.prototype.run = function (腿) {            if (腿 > 0) {                console.log(this.name + '是路上走');            }            else {                console.log(this.name + '是海里游');            };        };        fish.prototype = Object.create(animal.prototype);        fish.prototype.constructor = fish;        function fish(name, color, size) {            animal.call(this, 0);            this.name = name;            this.color = color;            this.size = size;        };        fish.prototype.eat = function () {            console.log(this.name + ' 乖乖吃');        };        cat.prototype = Object.create(animal.prototype);        cat.prototype.constructor = cat;        function cat(name, color, size) {            animal.call(this, 4);            this.name = name;            this.color = color;            this.size = size;        };        cat.prototype.eat = function () {            console.log(this.name + ' 喵喵吃');        };        var cat01 = new cat('kitty', 'brown', 'small');        var fish01 = new fish('Shark', 'RED', 'big');        console.log(cat01);        cat01.eat();        cat01.run(4);        console.log(fish01);        fish01.eat();        fish01.run(0);

http://img2.58codes.com/2024/20143762VUody06M9N.jpg


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章