首页 > 其他分享 >typescript: Prototype Pattern

typescript: Prototype Pattern

时间:2023-10-05 21:33:44浏览次数:31  
标签:typescript Pattern been cloned p1 str Prototype prototype

 

/**
 * Prototype Pattern 原型是一种创建型设计模式, 使你能够复制对象, 甚至是复杂对象, 而又无需使代码依赖它们所属的类。
 * The example class that has cloning ability. We'll see how the values of field
 * with different types will be cloned.
 */
class Prototype {

    public primitive: any;
    public ducomponent: object;
    public circularReference: ComponentWithBackReference;

    public clone(): this {
        const clone = Object.create(this);

        clone.component = Object.create(this.ducomponent);

        // Cloning an object that has a nested object with backreference
        // requires special treatment. After the cloning is completed, the
        // nested object should point to the cloned object, instead of the
        // original object. Spread operator can be handy for this case.
        clone.circularReference = {
            ...this.circularReference,
            prototype: { ...this },
        };

        return clone;
    }
}
/**
 * 
 */
class ComponentWithBackReference {
    public prototype;

    constructor(prototype: Prototype) {
        this.prototype = prototype;
    }
}

/**
 * The client code.
 */
function clientCodeproto() {
    const p1 = new Prototype();
    p1.primitive = 245;
    p1.ducomponent = new Date();
    p1.circularReference = new ComponentWithBackReference(p1);
    let str="";
    const p2 = p1.clone();
    //
    if (p1.primitive == p2.primitive) {
        console.log('Primitive field values have been carried over to a clone. Yay!');
        str=str+"Primitive ;";
        
    } else {
        console.log('Primitive field values have not been copied. Booo!');
        str=str+"not been copied;";
       
    }
    //
    if (p1.ducomponent == p2.ducomponent) {
        console.log('Simple component has not been cloned. Booo!');
        str=str+"Simple component not been cloned;";
       
    } else {
        console.log('Simple component has been cloned. Yay!');
        str=str+"Simple component;";
        
    }
    //
    if (p1.circularReference == p2.circularReference) {
        console.log('Component with back reference has not been cloned. Booo!');
        str=str+"Component not been cloned;";
    } else {
        console.log('Component with back reference has been cloned. Yay!');
        str=str+"Component been cloned;";
    }
    //
    if (p1.circularReference.prototype == p2.circularReference.prototype) {
        console.log('Component with back reference is linked to original object. Booo!');
        str=str+"Component to original object;";
    } else {
        console.log('Component with back reference is linked to the clone. Yay!');
        str=str+" Component to the clone;";
    }
    return str;
}

let strpro=clientCodeproto();
let strpro1="geovindu";
let messageprototype: string = 'Hello World,This is a typescript!,涂聚文 Geovin Du Web';
document.body.innerHTML = messageprototype+","+strpro+","+strpro1+",TypeScript 原型方法模式"

  

 

调用:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <head><title>TypeScript:原型模式Prototype Pattern</title>
      <meta name="Description" content="geovindu,涂聚文,Geovin Du"/>
<meta name="Keywords" content="geovindu,涂聚文,Geovin Du"/>
<meta name="author" content="geovindu,涂聚文,Geovin Du"/>  
    </head>
    <body>
        <script src="dist/prototypets.js"></script>
    </body>
</html>

  

输出:

 

https://www.typescriptlang.org/docs/handbook/modules.html
https://www.typescriptlang.org/docs/handbook/classes.html
https://www.koderhq.com/tutorial/typescript/get-set/
https://dev.to/jmalvarez/builder-pattern-in-typescript-3on0
https://github.com/josemiguel-alvarez/design-patterns-typescript/
https://www.sourcecodeexamples.net/2020/08/typescript-prototype-pattern-example.html
https://www.tutorialspoint.com/typescript/typescript_object_prototype.htm
https://sbcode.net/typescript/prototype/
https://dev.to/takaakit/uml-diagram-for-gof-design-pattern-examples-in-typescript-46d5
https://www.dofactory.com/javascript/design-patterns/prototype

 

标签:typescript,Pattern,been,cloned,p1,str,Prototype,prototype
From: https://www.cnblogs.com/geovindu/p/17743945.html

相关文章

  • typescript: Builder Pattern
     /***TypeScript实体类Model*BuilderPattern*生成器是一种创建型设计模式,使你能够分步骤创建复杂对象。*https://stackoverflow.com/questions/12827266/get-and-set-in-typescript*https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines*/cl......
  • 什么是 TypeScript 的类型增强功能
    TypeScript的类型增强(TypeAugmentation)是一种功能,它允许您扩展现有类型的成员,以添加新的属性或方法,以及修改已有属性或方法的类型定义。这个功能让您可以更好地适应第三方库或原始代码,以便在不修改源代码的情况下添加自定义的类型信息。在本文中,我将详细介绍TypeScript的类型增......
  • TypeScript入门到精通——TypeScript类型系统基础——单元类型、顶端类型、尾端类型
    单元类型 单元类型(UnitType)也叫作单例类型(SingletonType),指的是仅包含一个可能值的类型。由于这个特殊的性质,编译器在处理单元类型时甚至不需要关注单元类型表示的具体值。 TypeScript中的单元类型有以下几种:undefined类型null类型uniquesymbol类型void类型......
  • Flutter/Dart第09天:Dart高级特殊Pattern模式的概览和用法
    Dart官方文档:https://dart.dev/language/patterns重要说明:本博客基于Dart官网文档,但并不是简单的对官网进行翻译,在覆盖核心功能情况下,我会根据个人研发经验,加入自己的一些扩展问题和场景验证。Pattern模式匹配的定义官网定义:PatternsareasyntacticcategoryintheDartlan......
  • TypeScript入门到精通——TypeScript类型系统基础——字面量类型
    字面量类型 TypeScript支持将字面量作为类型使用,我们称之为字面量类型。每一个字面量类型都只有一个可能的值,即字面量本身。1、boolean字面量类型 boolean字面量类型只有以下两种:true字面量类型false字面量类型 原始类型boolean等同于由true字面量类型......
  • 什么是 TypeScript 的类型增强功能
    TypeScript的类型增强(TypeAugmentation)是一种功能,它允许您扩展现有类型的成员,以添加新的属性或方法,以及修改已有属性或方法的类型定义。这个功能让您可以更好地适应第三方库或原始代码,以便在不修改源代码的情况下添加自定义的类型信息。在本文中,我将详细介绍TypeScript的类型......
  • 如何使用 TypeScript 的 module augmentation 技术增强 Spartacus Feature Library
    moduleaugmentation技术是一种强大的TypeScript功能,它允许开发人员在不修改原始代码的情况下扩展现有模块的功能。这种技术在Angular生态系统中的应用尤为广泛,特别是在构建功能库和插件时,以确保代码的可维护性和可扩展性。概述Moduleaugmentation允许我们向现有模块添加......
  • eslint airbnb React18+typescript 依赖循环、import自动排序分组
    eslint终极规范爱彼迎eslint-config-airbnb请先阅读完下以下链接在来配置代码规范之什么是eslint,为什么要使用eslinteslint的配置项过多,针对js、ts、vue、jsx、tsx等等不同的规则,小公司或者个人项目可以使用成熟的eslint社区规范,如airbnb、standard、goole等。这里我们介绍......
  • ypeScript入门到精通——TypeScript类型系统基础——枚举类型
    TypeScript类型系统基础——枚举类型 枚举类型由零个或多个枚举成员构成,每个枚举成员都是一个命名的常量。 在TypeScript中,枚举类型是一种原始类型,它通过enum关键字来定义。例如,我们可以使用枚举类型来表示一年四季,示例如下:enumSeason{Spring,Summer,......
  • 【愚公系列】2023年10月 二十三种设计模式(一)-工厂方法模式(Factory Method Pattern)
    ......