Angular2中实现基于TypeScript的对象合并方法:extend()

摘要:
TypeScript里面没有现成的合并对象的方法,这里借鉴jQuery里的$.extend()方法。写了一个TypeScript的对象合并方法,使用方法和jQuery一样。部分代码和jQuery代码略有不同,主要是判断元素是否为数组和纯对象的部分。1publicclass2type={};2ngOnInit(){3this.getClass2type();4}56/**7*对象拷贝,参考$.extend()实现。首个参数为true时为深度拷贝,默认为false。'array':typeofsrc;43//不能用typeof判断一个数组是否为数组格式,例:typeof[]-˃object。

TypeScript里面没有现成的合并对象的方法,这里借鉴jQuery里的$.extend()方法。写了一个TypeScript的对象合并方法,使用方法和jQuery一样。

部分代码和jQuery代码略有不同,主要是判断元素是否为数组纯对象的部分。jQuery中有方法可直接判断元素是否为数组($.isArray())和对象($.isPlainObject()),但是TpyeScript里面没有,这里按照jQuery的实现写了一下判断,大部分情况应该没问题,但不保证适用所有情况。感兴趣的话可以体会一下,遇到什么问题一起讨论一下。

1   public class2type ={};
2 ngOnInit() {
3     this.getClass2type();
4 }
5  
6   /**
7 * 对象拷贝,参考$.extend()实现。首个参数为true时为深度拷贝,默认为false。
8 *
9 * @param {any} args
10 * @returns
11 * @memberof SharedService
12    */
13 extend(...args) {
14 let options, name, src, srcType, copy, copyType, copyIsArray, clone,
15       target = args[0] ||{},
16       i = 1,
17       length =args.length,
18       deep = false;
19      
20     if ( typeof target === 'boolean') {
21       deep =target;
22       target = args[i] ||{};
23       i++;
24 }
25     if ( typeof target !== 'object' && typeof target !== 'function') {
26       target ={};
27 }
28     if ( i ===length) {
29       target = this;
30       i--;
31 }
32     for ( ; i < length; i++) {
33       if ( (options = args[i]) !== null) {
34         for ( name inoptions ) {
35           src =target[name];
36           copy =options[name];
37           //若参数中字段的值就是目标参数,停止赋值,进行下一个字段的赋值
38           //这是为了防止无限的循环嵌套
39           if ( target ===copy ) {
40             continue;
41 }
42           srcType = this.isArray(src) ? 'array': typeofsrc;
43           //不能用typeof判断一个数组是否为数组格式,例:typeof [] -> object。如需判断的话可用'[] instanceof Array'方法。
44           //copyType = typeof copy;
45           if ( deep && copy && ((copyIsArray = this.isArray(copy)) || typeof copy === 'object')) {
46             if( copyIsArray ) {
47               copyIsArray = false;
48               clone = src && srcType === 'array' ?src : [];
49             } else{
50               clone = src && srcType === 'object' ?src: {};
51 }
52             target[name] = this.extend(deep, clone, copy);
53           } else if ( copy !==undefined ) {
54             target[name] =copy;
55 }
56 }
57 }
58 }
59     returntarget;
60 }
61  
62   public isArray = Array.isArray || function(obj) {
63     return this.type(obj) === 'array';
64 }
65  
66 private type(obj: object) {
67     if (obj === null) {
68       return obj + "";
69 }
70     return typeof obj === 'object' || typeof obj === 'function' ?
71         this.class2type[this.toString.call(obj)] || 'object':
72         typeofobj;
73 }
74  
75 private getClass2type() {
76     'Boolean Number String Function Array Data RegExp Object Error'.split(' ').forEach(name =>{
77       this.class2type['[object' + name + ']'] =name.toLowerCase();
78 });
79 }
80  
81   //深度遍历,使用方法:
82   let newObj = this.extend(true, {}, objA, objB);

免责声明:文章转载自《Angular2中实现基于TypeScript的对象合并方法:extend()》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇第一篇-ubuntu18.04访问共享文件夹Cadence学习笔记下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

利用.NET4.0的DynamicObject来拦截调用实现AOP

利用AOP我们很容易的就能实现类似Log等业务无关的横切关注点,避免了这些代码在项目中不断的重复,利用.NET DLR 我们可以很容易的就实现一个简单的AOP,代码如下: using System;using System.Dynamic;using System.Reflection;namespace Zxf.Practices{public clas...

opencv linux

http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html https://www.google.com.hk/search?q=opencv+linux&ie=utf-8&oe=utf-8&gws_rd=cr&ei=f...

Deno会在短期内取代Node吗?

转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 原文出处:https://blog.bitsrc.io/what-is-deno-and-will-it-replace-nodejs-a13aa1734a74   Deno是什么? Deno v1.0.0已于5月13日正式发布。 其开发者为Ryan Dahl...

C#中的typeof()和GetType()的区别

说明:这里只列举了一部分,还有待继续添加: 1、typeof(x)中的x,必须是具体的类名、类型名称等,不可以是变量名称。 2、GetType()方法继承于Object(C#中所有类的基类都是object类。基类是相对于派生类而言的,比方说:如果B类继承自A类,则A就是B的基类。), 所以C#中任何对象都具有GetType()方法,它的作用和typeof(...

javascript console

javascript console console.log(object[, object, ...])在控制台输出一条消息。如果有多个参数,输出时会用空格隔开这些参数。 第一个参数可以是一个包含格式化占位符输出的字符串,例如: console.log("The %s jumped over %d tall buildings", animal, co...

mac 下 ts 环境 配置

1. 打开iterm ,输入 sudo npm install -g typescript 2. 复制 安装的 路径 3. 输入 vim ~/.bash_profile 4.export PATH=${PATH}:/Users/farben/.npm-global/lib/node_modules/typescript/bin/ 5.按esc 退出,:wq...