关于前端:TypeScript中如何使用Objectobject和

6次阅读

共计 3226 个字符,预计需要花费 9 分钟才能阅读完成。

Object、object 和{}(对象类型)

不晓得是不是有很多兄弟和我一样,在进行 typescript 的前端我的项目开发时,总是不能很好地应用 对象 类型;有时会定义为Object,有时又会定义为object,然而大多时候咱们会混同两者。

就拿我集体而言,对于对象我都下意识的定义为object,运气好呢编译通过了(开心😄),运气差点呢 vscode 还有提醒修复能,一键修复也是记极好了。然而,咱也不是完事就不负责的人呀,为了心里的那份责任,这些用法的差别明天就非弄懂不可了(💪🏻),接下来那就开始吧!!

Object

Object 类型是所有 Object 类的实例的类型。它由以下两个接口来定义:

  • Object 接口定义了 Object.prototype 原型对象上的属性;
  • ObjectConstructor 接口定义了 Object 类的属性。

如何了解上述两点呢?

Object 是一个对象,然而是蕴含了 js 原始的所有专用的性能,这个须要去理解 js 的原型链(这里不过多进行讲述),从 TypeScript 源码进行剖析:

interface Object {
   /** The initial value of Object.prototype.constructor is the standard built-in Object constructor. */
   constructor: Function;
   /** Returns a string representation of an object. */
   toString(): string;
   /** Returns a date converted to a string using the current locale. */
   toLocaleString(): string;
   /** Returns the primitive value of the specified object. */
   valueOf(): Object;
   /**
    * Determines whether an object has a property with the specified name.
    * @param v A property name.
    */
   hasOwnProperty(v: PropertyKey): boolean;
   /**
    * Determines whether an object exists in another object's prototype chain.
    * @param v Another object whose prototype chain is to be checked.
    */
   isPrototypeOf(v: Object): boolean;
   /**
    * Determines whether a specified property is enumerable.
    * @param v A property name.
    */
   propertyIsEnumerable(v: PropertyKey): boolean;
}
interface ObjectConstructor {
 /** Invocation via `new` */
 new(value?: any): Object;
 /** Invocation via function calls */
 (value?: any): any;
 readonly prototype: Object;
 getPrototypeOf(o: any): any;
 // ···
}
declare var Object: ObjectConstructor;

从中能够看出,Object 的构造函数指向了 Function,在学习 js 原型链时咱们晓得Object 和 Function 是互相指向对方的

Object 类型能够通过 new 进行创立;

留神:Object 类型蕴含了所有的原始 / 根底类型,所以能够给 Object 类型赋值为根底类型;如果值对象属性名与 Object 接口中的属性抵触,则 TypeScript 编译器会提醒相应的谬误:如下例,对象中重写了 toString 办法,然而返回类型和 Object 中返回类型 string 抵触,所以报错;

let obj: Object; // 或者 let obj = new Object();
obj = "hell oworld";
obj = 1;
obj = true;
obj = undefined; //Error:Type 'undefined' is not assignable to type 'Object'.
obj = {
      // Type 'number' is not assignable to type 'string'
    toString() {return 123;}
}

数组型 Object => Object[]:因为 Object 蕴含原始类型,所以上面代码编译不会报错

let obj: Object[];
obj = [
    123,
    true,
    "hello world",
    [1, 'a', false]
];

object

object 类型是:TypeScript 2.2 引⼊的新类型,它⽤于示意 ⾮原始类型 。object 是键值对汇合,非凡在 值也必须是 object

留神:object 类型默认能够应用在 Object 类型上定义的所有属性和办法,这些属性和办法可通过 JavaScript 的原型链隐式地应用,然而如果在 object 中重写了原型链中的属性或者办法,那么会间接笼罩,不受原型链上的影响!

let obj: object; 
obj = 1; // Error:Type 'number' is not assignable to type 'object'.
obj = true; // Error: Type 'boolean' is not assignable to type 'object'.
obj = 'a'; // Error: Type 'string' is not assignable to type 'object'.
obj = undefined; //Error:Type 'undefined' is not assignable to type 'object'.
obj = {
    a : "hell oworld",
    b : 1,
    c : true,
}

obj = {toString() {return 123;}
}
console.log(obj.toString()) // 123

{}/ 空类型

空类型:{}。它形容了一个没有成员的对象,在 typeScript 中能够有以下形式生成空类型:

  • 1、没有申明变量类型,然而初始值为{}

    let obj = {}; 
  • 间接申明变量类型为{}

    let obj: {}; 

当你试图拜访这样一个对象的任意属性时,TypeScript 会产生一个编译时谬误;然而,你依然能够使⽤在 Object 类型上定义的所有属性和⽅法,这些属性和⽅法可通过 JavaScript 的原 型链隐式地使⽤:

let obj: {}; 
obj = undefined; //Error:Type 'undefined' is not assignable to type '{}'.
obj = 'a';
obj = {
    a : "hell oworld",
    b : 1,
    c : true,
    toString() {return 123;}
}
console.log(obj) 
/*
{
  "a": "hell oworld",
  "b": 1,
  "c": true
} 
*/
console.log(obj.toString()) // 123;

总结(比拟)

对于 Object、object 和{},三者都能够使⽤在 Object 类型上定义的所有属性和⽅法,这些属性和⽅法可通过 JavaScript 的原 型链隐式地使⽤;并且都不能被赋值为 undefined、null 类型;

Object vs object:

  • 1、两者原型上属性办法重写体现不统一;
  • 2、object 类型值示意 ⾮原始类型,Object 类型值能够为原始类型;
  • 3、Object 能够通过 new 来定义类型;

Object vs {}:

  • 1、两者类型值能够为原始类型;
  • 2、两者原型上属性办法重写体现不统一;
  • 3、Object 能够通过 new 来定义类型;

object vs {}:

  • 1、两者原型上属性办法重写体现统一;
  • 2、object 类型值示意 ⾮原始类型,{} 类型值能够为原始类型;
正文完
 0