简体   繁体   English

JavaScript中对象与对象之间的区别

[英]difference between object and Object in JavaScript

In the below code :- 在下面的代码中:

 var x = { } ;
    console.log(typeof x);             // o/p is object
    console.log(x instanceof Object ); //o/p is true

If I use " object " instead of " Object " in the last line I get an error.Why is that so when the o/p of second line is object with a lowercase "o"? 如果我在最后一行中使用“ 对象 ”而不是“ 对象 ”,则会出现错误。为什么当第二行的o / p是带有小写字母“ o”的对象时呢?

Because there's no such thing as an 'object'. 因为没有“对象”之类的东西。 Typeof doesn't give you the class back - it gives you back the primitive type that it is. Typeof不会给您类,而是会给您原来的类型。 For example, typeof "string" gives you back "string". 例如, typeof "string"会给您返回“ string”。

The 'Object' is a constructor for an object 'primitive' - so a new Object gives you back an 'object' to work with.. but don't expect to be able to create a 'new object', as an 'object' doesn't exist as a constructor. “对象”是对象“原始”的构造函数-因此, new Object为您提供了可以使用的“对象”。但是不要期望能够创建“新对象”作为“对象” '作为构造函数不存在。

You get an error because you haven't defined a variable named object . 由于未定义名为object的变量,因此会出现错误。 Attempting to read a variable that has not been declared is a ReferenceError . 尝试读取尚未声明的变量是ReferenceError

The Object variable is native to the environment, and is pre-defined as the constructor function for plain objects. Object变量是环境固有的,并且预先定义为普通对象的构造函数。 That's why it works when you do instanceof Object . 这就是为什么当您执行instanceof Object时它可以工作的原因。 Every native object in JavaScript is an instance of Object . JavaScript中的每个本机对象都是Object一个实例。

Javascript is case sensitive "object" is essentially a variable that can hold anything. JavaScript是区分大小写的“对象”,本质上是一个可以容纳任何内容的变量。 "Object" is an actual javascript type. “对象”是实际的javascript类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM