【问题标题】:What is the difference between {} and Object?{} 和 Object 有什么区别?
【发布时间】:2018-06-20 11:12:24
【问题描述】:

最近,我对{}Object 之间的区别感到困惑。有时,{} 会解决我的问题,有时,它不能,我切换到Object。我真的不知道为什么。

我做了一些测试,希望能给你一些提示。

const t: Array<{label: string}> = [{label:'1'}];
const arr: Array<{}> = t; //error
const arr2: Array<Object> = t; //pass

【问题讨论】:

  • 最小化你的例子。
  • {}new Object() 的别名

标签: javascript flowtype


【解决方案1】:

{}new Object() 的别名。

所以你可以说Object 是一个class{} 是那个类的一个instance

你可以在这里看到:

console.log(JSON.stringify(new Object()) == JSON.stringify({}))

console.log({} instanceof Object)

【讨论】:

  • 他在询问流类型,而不是 javascript 对象
【解决方案2】:

我觉得这个答案比较合理Github link:[mixed type] Supertype bug

数组在 Flow 中是不变的

class A {}
class B extends A {}
var bs: Array<B> = [];
var as: Array<A> = bs;
as.push(new A); // this would be bad!

如果AA 的超类型,我断言Array&lt;B&gt; 应该是Array&lt;A&gt; 的子类型, 不是真的

Object是个例外,

Object 类型是所有对象的 supertypesubtype。这意味着Object 不是原生/内置 Object 类型的严格等价物,而是更类似于any

@Tushar Acharekar 和 @Ayush Gupta,感谢您的回答。

【讨论】:

    【解决方案3】:

    对象是属性的集合,属性是名称(或键)与值之间的关联。属性的值可以是函数,在这种情况下,属性称为方法。

    • 我更喜欢使用{}来生成key:value对数据。
    • 我更喜欢使用new Object() 来创建像Method1, Method2 这样的复杂对象

    请看下面的例子

    var d = new Object();           //This is the simplest way to create an empty object.
    var a = Object.create(null);    //This method creates a new object extending the prototype object passed as a parameter.
    var b = {};                     //This is equivalent to Object.create(null) method, using a null prototype as an argument.
    

    方法一:

    var Animal = {
      type: 'Invertebrates',  // Default value of properties
      displayType: function() {  // Method which will display type of Animal
        console.log(this.type);
      }
    };
    var animalObject = Object.create(Animal);
    animalObject.displayType();         // Output:Invertebrates
    var fish = Object.create(Animal);
    fish.type = 'Fishes';
    fish.displayType();                 // Output:Fishes
    

    方法二:

    var Obj = function(name) {
      this.name = name
    }
    var c = new Obj("hello"); 
    

    Dicoverign Javascript 是学习的最佳视频javascript prototype

    【讨论】:

    • 他在询问流类型,而不是 javascript 对象
    • @AlekseyL。我知道这一点,所有者已经改变了他的问题上下文,但根据标题,我试图解释 {}Object 中的用法差异@
    【解决方案4】:

    我没有使用过Flow,但我在这里尝试了你的代码:https://flow.org/try/,我收到了这条消息:Type argument 'T' is incompatible。然后它添加变量Property 'label' is incompatibleProperty not found

    我猜是因为tSealed Objects 的数组,但arrUnsealed Objects 的数组。

    现在,当您将 t 分配给 arr2 时它可以工作,因为 arr2 只是一个(纯 Javascript)对象数组。您也可以将一组未密封的对象分配给arr2。或者推送到arr2 混合的密封和未密封对象。

    注意这些通道:

    const t: Array<{label: string}> = [{label:'1'}];
    const t1: {label: string} = {label: `1`};
    const w: Array<{label: string}> = [t1];
    const t2: {label: string} = {label: `2`};
    w.push(t2);
    

    但这不会通过:

    let w: Array<{label: string}> = [];
    const t3: {} = {};
    w.push(t3);
    

    但这些都会过去的:

    const u: Array<Object> = [{label:'1'}];
    const arr: Array<{}> = u;   
    
    const v: Array<{}> = [{label:'1'}];
    const arr2: Array<Object> = v;  
    
    const t3: {foo: number} = {foo:1};
    arr2.push(t3);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-31
      • 2011-08-12
      • 1970-01-01
      • 2019-12-06
      • 2013-04-11
      • 2011-11-25
      • 1970-01-01
      • 2014-09-03
      相关资源
      最近更新 更多