加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

JS设计模式:安全类 继承 多继承 多态

发布时间:2020-12-15 00:31:14 所属栏目:C语言 来源:网络整理
导读:安全类 var Book = function() { if(this instanceof Book) { this.title = title; this.time = time; this.type = type; } else { return new Book(title,time,type); }} 继承 类式继承 function SuperClass() { this.books = ['JS','html','css'];} functi

安全类

var Book = function() {
    if(this instanceof Book) {
        this.title = title;
        this.time = time;
        this.type = type;
    } else {
        return new Book(title,time,type);
    }
}

继承

类式继承

function SuperClass() {
    this.books = ['JS','html','css'];
}

function SubClass() {}

Subclass.prototype = new SuperClass();

var instance1 = new SubClass();
var instance2 = new SubClass();
console.log(instance2.books);
instance1.books.push('xiaoming');
console.log(instance2.books);

console.log(SubClass.prototype instanceof SuperClass);

instance1的一个有意无意修改就破坏了instance2的book属性。

//Shape - superclass
function Shape() {
  this.x = 0;
  this.y = 0;
}

Shape.prototype.move = function(x,y) {
this.x += x;
this.y += y;
console.info("Shape moved.");
};

// Rectangle - subclass
function Rectangle() {
Shape.call(this); //call super constructor.
}

Rectangle.prototype = Object.create(Shape.prototype);

var rect = new Rectangle();

rect instanceof Rectangle //true.
rect instanceof Shape //true.

rect.move(1,1); //Outputs,"Shape moved."

组合式继承

类式继承与构造函数继承的组合

function SuperClass(name) {
    this.name = name;
    this.books = ['JS','css'];
}

SuperClass.prototype.getName = function() {
console.log(this.name);
}

function SubClass(name,time) {
SuperClass.call(this,name);
this.time = time;
}

SubClass.prototype = new SuperClass();
SubClass.prototype.getTime = function() {
console.log(this.time);
};

var instance1 = new SubClass('js book',2014);
instance1.books.push('xiaoming');
instance1.getTime();
instance1.getName();
var instance2 = new SubClass('css book',2013);
console.log(instance2.books);
instance2.getTime();
instance2.getName();

子类构造函数中执行父类构造函数, 在子类原型上实例化父类就是组合模式。(prototype中的相同属性被覆盖掉了,因为实例先从自身的属性找。)

纯净式继承

var inheritPrototype = function(parent,child) {
    var F = function() {};
    F.prototype = parent.prototype;
    child.prototype = new F();
    child.prototype.constructor = child;
}

多继承

var inheritPrototype = function(parent,child) {
    var F = function() {};
    F.prototype = parent.prototype;
    child.prototype = new F();
    child.prototype.constructor = child;
}

function SuperClass(name) {
this.name = name;
this.books = ['JS','css'];
}
SuperClass.prototype.getName = function() {
console.log(this.name);
}

function SubClass(name,time) {
this.time = time;
}
inheritPrototype(SuperClass,SubClass);
var instance1 = new SuperClass('js book',2014);
var instance2 = new SubClass('js book',2014);

console.log('test...')
for(var property in SuperClass) {
console.log(property); // ^-^Nothing (SuperClass当前只是一个构造函数)
}

for(var property in instance1) {
console.log(property); // name books getName (原型上的也包括)
}

for(var property in instance2) {
console.log(property); // time constructor getName (constructor更改过就显示,否则默认不显示)
}

var extend = function(target,source) {
    for (var property in source) {
        target[property] = source[property];
    }
    return target;
}

原型继承

var Person = function(name,work) {
    var _person = new Human();
    _person.name = new Named(name);
    _person.work = new Work(work);
    this = _person;
}

var person = new Person('xiaoming','code');

实例间的 继承。

多态

function Add() {
    function zero() {
        return 10;
    }
function one(num) {
    return 10 + num;
}

function two(num1,num2) {
    return num1 + num2;
}

this.add = function() {
    var arg = arguments,len = arg.length;

    switch(len) {
        case 0:
            return zero();
        case 1:
            return one(arg);
        case 2:
            return two(arg[0],arg[1]);
    }
}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读